0

我想动态修改单元格的内容,即使在重新加载数据后我也无法正常工作。正如预期的那样,当视图加载时,我在桌子上垂直得到 1 1 1 1 1 1 1,当我按下段 1 时,我得到 4 4 4 4 4 4 4 正如预期的那样,但是,当我按下 2 时,我得到 -1 - 1 -1 -1 -1 -1 再次,当我按 3 时,我再次得到 4 4 4 4 4 4 4。此外,我的单元格的内容取决于我按下按钮的顺序。我的代码在下面的表格中只有一行和许多部分:

 - (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";




       }

      }
        }
4

1 回答 1

0

您需要将配置单元格的代码移到if (cell == nil)块之外,以便在您重新加载单元格时执行它并通过以下方式回收它们dequeueReusableCellWithIdentifier:

if (cell == nil) {

    cell = [[MyTableCell alloc] initWithStyle:UITableViewCellStyleDefault
                          reuseIdentifier:MyIdentifier] ;
}

if(tableView==byTeam) {
    label.text=[currentarray objectAtIndex:0];
    //...
}

此外,除非我遗漏了什么,否则您的分段控制处理程序可以通过消除buttonPressed变量来简化很多。像这样的东西,例如:

- (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];
   }
}     
于 2013-05-31T15:04:41.787 回答