Search code examples
iosobjective-cuitableviewuipickerview

Update UITableView cells depending on UIPickerView


I have an UIPickerView

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView     
{     
 return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
{
 return seasons.count;
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
{
 if ([season selectedRowInComponent:0] == 6) {
    matches = [[NSMutableArray alloc] initWithCapacity: 20];
    [matches insertObject:[NSMutableArray arrayWithObjects:@"aaa", @"bbb", @"1-0", @"15-03-2010", nil] atIndex:0];
 } else if
 .
 .
 .
}

Then I have an UITableView

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
 return matches.count;
}

- (CustomCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{     
 static NSString *simpleTableIdentifier = @"customCell";

 CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

 if (cell == nil) {
    cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
 }
 cell.team1.text = matches[indexPath.row][0];
 cell.team2.text = matches[indexPath.row][1];
 cell.score.text = matches[indexPath.row][2];

 return cell;
}

So right now the UITableView is filled with the objects from the matches array that I initialize in my viewDidLoad. How can I update the cells content depending on the selected row of the UIPickerView ?


Solution

  • Simply call [self.tableView reloadData]; after you update the matches array.

    -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
    {
     if ([season selectedRowInComponent:0] == 6) {
        matches = [[NSMutableArray alloc] initWithCapacity: 20];
        [matches insertObject:[NSMutableArray arrayWithObjects:@"aaa", @"bbb", @"1-0", @"15-03-2010", nil] atIndex:0];
     } else if
     .
     .
     .
    
     [self.tableView reloadData];
    }
    

    This assumes that self is both the table view data source/delegate and the picker view data source/delegate