使用 pickerview 填充 tableview

Populate tableview with pickerview

我有一个按钮、一个 pickerview 和一个 tableview。当我按下按钮时,pickerview 的当前选择将填充到 table 视图中。这是从 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 等填充到我的 tableview

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

 ...Content from above msgCourse and etc

    return cell;

}

请问如何弥补我第二部分的差距?或者有什么例子可以看吗?

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

第一步: 如果您还没有通过故事板完成委派,这是您应该以编程方式做的第一件事:

  -(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/