繁体   English   中英

用pickerview填充tableview

[英]Populate tableview with pickerview

我有一个按钮,一个pickerview和一个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等填充到我的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/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM