【问题标题】:Populate tableview with pickerview用pickerview填充tableview
【发布时间】:2015-04-06 18:46:21
【问题描述】:

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

}

请问如何解决我第二部分的空白?或者有什么可以看的例子吗?

【问题讨论】:

    标签: ios uitableview ios7 uipickerview


    【解决方案1】:

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

    第一步:如果您还没有通过情节提要完成委托,那么您应该以编程方式进行的第一件事:

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

    【讨论】:

    • 非常感谢您的回复。它在 cell.textLabel.text = msgCourse 上报告错误;我认为 msgCourse 是一个局部变量?再次感谢。
    • 感谢您的耐心等待。如果我需要在多行表格视图中填充多条消息,我需要修复一些问题吗?
    • 我的代码已更新见上文。但是你应该把它设为 NSMutableArray
    • 非常感谢。在这里很有帮助。
    • 不客气,我还在回答中添加了教程。很高兴跟随。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-08
    • 2018-10-14
    相关资源
    最近更新 更多