Search code examples
iosuitableviewios7uipickerview

Populate tableview with pickerview


I have a button, a pickerview and a tableview. When I push the button, the current selection of pickerview will be filled in the table view. Here is the code of button which pick value from 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];

}

Then I want to populate msgCourse,etc to my tableview

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

 ...Content from above msgCourse and etc

    return cell;

}

How to fix the gap in my second part please? Or any example to look at please?


Solution

  • Check the following code to decide whether or not that fits into your requirements based on what I understand from your question. If it does not, please leave a comment then I will try my best to follow up.

    First Step: if you have not done the delegation via storyboard, here is the first thing that you should do programatically:

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

    Second Step: I have not seen a mutablearray in your code, but I assume that you are going to make msgCourse to 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;
    }
    

    Last Step: By the way, do not forget to add the following line of code into your button action to reload your tableView, I assume you update the NSMutableArray and would like to refresh the tableView.

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

    Here is the good tutorial, it is worth to duplicate for the sake of learning: http://www.appcoda.com/ios-programming-tutorial-create-a-simple-table-view-app/