ios - 使用 pickerview 填充 tableview

标签 ios uitableview ios7 uipickerview

我有一个按钮、一个选择器 View 和一个表格 View 。当我按下按钮时,选择器 View 的当前选择将填充到 TableView 中。这是从pickerview中选取值的按钮的代码

- (IBAction)addCourse:(UIButton *)sender {

    NSInteger numRow=[picker selectedRowInComponent:kNumComponent];//0=1st,1=2nd,etc
    NSInteger SeaRow=[picker selectedRowInComponent:kSeaComponent];//0=fall,1=spring,2=summer
    NSInteger CourseRow=[picker selectedRowInComponent:kCourseComponent];

    NSString *num=Number[numRow];
    NSString *season=Season[SeaRow];
    NSString *course=Course[CourseRow];

    NSString *msgCourse=[[NSString alloc ]initWithFormat:@"%@ ",course];
    NSString *msgSeason=[[NSString alloc ]initWithFormat:@"%@ ",season];
    NSString *msgYear=[[NSString alloc ]initWithFormat:@"%@ ",num];

}

然后我想将 msgCourse 等填充到我的表格 View

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

 ...Content from above msgCourse and etc

    return cell;

}

请问如何修复第二部分中的空白?或者有什么例子可以看吗?

最佳答案

根据我从您的问题中了解到的内容,检查以下代码以确定它是否符合您的要求。如果没有,请发表评论,我会尽力跟进。

第一步:如果您还没有通过 Storyboard完成委托(delegate),那么您应该以编程方式做的第一件事是:

  -(void)ViewDidLoad
  {  
    tableView.delegate = self;
    tableView.datasource = self;
   }

第二步:我没有在您的代码中看到 mutablearray,但我假设您要将 msgCourse 设置为 NSMutableArray。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// I assume you have only one section
// Return the number of sections.
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

// Return the number of rows in the section.
 return [self.msgCourse  count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = nil;
    cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

    if(!cell){
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
        }

    cell.textLabel.text=[self.msgCourse objectAtIndex:indexPath.row];
    return cell;
}

最后一步: 顺便说一句,不要忘记将以下代码行添加到您的按钮操作中以重新加载您的 tableView,我假设您更新了 NSMutableArray 并想刷新 tableView。

- (IBAction)addCourse:(UIButton *)sender {
   .....
   .....
   .....
   [tableView reloadData];
}

这是一个很好的教程,值得复制以供学习:http://www.appcoda.com/ios-programming-tutorial-create-a-simple-table-view-app/

关于ios - 使用 pickerview 填充 tableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29477521/

相关文章:

objective-c - 整数未存储在数据库中(sqlite3)

ios7 - 如何在 IOS 7 中通过相机捕获图像后跳过重拍/使用照片选项

ios - iOS 上 MKMapSnapshotter 的 VectorKit 崩溃报告

ios - iOS 是否存在导致应用程序崩溃的内存限制?

ios - XCode 在项目/文件级别的本地化/国际化自动化,同时适用于所有字符串/数字/单位

iphone - 当应用程序重新启动时,如何保留 UITextField 中存储在 plist 中的数据?

iphone - UITableView didSelectRowAtIndexPath : Not Working

ios - 设置 AppleLanguages 在 iOS 7 上不起作用?

objective-c - UIView 未检测到横向触摸

ios - NSFetchedResultsController performFetch 函数解码所有对象