动态修改单元格文本

问题描述:

我想动态修改单元格的内容,即使在重新加载数据后,我也没有真正的工作。正如所料,当视图加载时,我在桌子上垂直放置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"; 




     } 

     } 
     } 

你需要移动配置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]; 
    } 
}  
+0

非常感谢你,工作就像一个魅力!竖起大拇指! – snowleopard

+0

我知道你已经做了,但你能否请你进一步解释为什么我不得不将它移出cell = nil语句。问候 – snowleopard

+0

当然。当你重新加载一个单元格时,'dequeueReusableCellWithIdentifier:'回收你重新加载并返回的单元格之一。所以你的'cell'变量在你进入'if(cell == nil)'语句之前已经设置好了,所以程序永远不会进入该块。它在第一次传递时工作正常,因为最初没有要回收的单元格,并且'dequeueReusableCellWithIdentifier:'返回'nil',从而导致程序进入该块。 –