Search code examples
uitableviewcelluisegmentedcontrol

Modify cell text dynamically


I want to modify the content of my cell dynamically, my doesn't really work even after reloading the data. As expected, when the view loads i get 1 1 1 1 1 1 1 vertically on the table, when i press segment 1 i get 4 4 4 4 4 4 4 as expected, however, when i press 2, i get -1 -1 -1 -1 -1 -1 again and when i press 3 i get 4 4 4 4 4 4 4 again. Also, the content of my cells depends on sequence in which i press the buttons. My code is below tables have only one row and numerous sections:

 - (void)viewDidLoad
{
currentarray=[NSArray arrayWithObjects:@"-1",@"-2",@"-3",  @"-4", nil];
 }
- (IBAction)SegControl: (id) sender{

for(int i=0; i<4; i++){
    if(theSegControl.selectedSegmentIndex==i){
        buttonpressed[i]=[NSNumber numberWithInt:1];
    }
    else {
        buttonpressed[i]=[NSNumber numberWithInt:0];
    }
}

if([buttonpressed[0] intValue]==1){

    currentarray=sorteddectotalteamPoints;

}

else if([buttonpressed[1] intValue]==1){

    currentarray=[NSArray arrayWithObjects:@"4",@"6",@"7",  @"8", nil]; NSLog(@"%@",@"two pressed");
}

else if([buttonpressed[2] intValue]==1){

    currentarray=[NSArray arrayWithObjects:@"9",@"10",@"11",  @"12", nil]; NSLog(@"%@",@"three pressed");
}

else {

    currentarray=[NSArray arrayWithObjects:@"13",@"14",@"15",  @"16", nil]; NSLog(@"%@",@"four pressed");
}

 //to update the cells    
for(int i=0; i<[teamnameswithoutRepeat count];i++){
    [byTeam reloadSections:[NSIndexSet indexSetWithIndex:i] withRowAnimation:UITableViewRowAnimationFade];
}
  NSLog(@"%@",currentarray);


  }

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

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, tableView.rowHeight)] ;

label.font = [UIFont fontWithName:@"Helvetica-Bold" size:14];
label.layer.borderColor = [UIColor whiteColor].CGColor;
label.layer.borderWidth = 1.0;


label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor blackColor];
 NSString *MyIdentifier = [NSString stringWithFormat:@"MyIdentifier %i", indexPath.section];

MyTableCell *cell = (MyTableCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];

if (cell == nil) {

    cell = [[MyTableCell alloc] initWithStyle:UITableViewCellStyleDefault
                              reuseIdentifier:MyIdentifier] ;
    if(tableView==byTeam)
    {

         //[tableView reloadData];
        label.text=[currentarray objectAtIndex:0];
        [cell.contentView addSubview:label];
        label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
        UIViewAutoresizingFlexibleHeight;
       // UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
        //cell.textLabel.text = @"updated text";




       }

      }
        }

Solution

  • You need to move the code that configures the cell outside of the if (cell == nil) block so that it gets executed when you reload cells and they get recycled by dequeueReusableCellWithIdentifier::

    if (cell == nil) {
    
        cell = [[MyTableCell alloc] initWithStyle:UITableViewCellStyleDefault
                              reuseIdentifier:MyIdentifier] ;
    }
    
    if(tableView==byTeam) {
        label.text=[currentarray objectAtIndex:0];
        //...
    }
    

    Also, unless I'm missing something, your segmented control handler can be simplified quite a bit by eliminating the buttonPressed variable. Something like this, for example:

    - (IBAction)SegControl:(id)sender
    {
        switch(sender.selectedSegmentIndex) {
            case 0:
                currentArray = sorteddectotalteamPoints;
                break;
            case 1:
                currentArray = [NSArray arrayWithObjects:@"4",@"6",@"7",  @"8", nil];
                break;
            case 2:
                currentArray = [NSArray arrayWithObjects:@"9",@"10",@"11",  @"12", nil];;
                break;
            case 3:
                currentArray = [NSArray arrayWithObjects:@"13",@"14",@"15",  @"16", nil];
                break;
        }
    
       //to update the cells    
       for(int i=0; i<[teamnameswithoutRepeat count];i++) {
           [byTeam reloadSections:[NSIndexSet indexSetWithIndex:i] withRowAnimation:UITableViewRowAnimationFade];
       }
    }