2011-08-24 57 views
0

以下代码设置单元格文本并仅为上次选中的添加校验标记。总是只有一个单元格检查被标记并且正常工作,除了第一次显示时。因此,直到您按任何其他单元格时,文本才会显示在该单元格中(仅限那个单元格)。例如,如果cellPos = 4时viewDidLoad,该单元格将不包含文本。UITableView选中的单元格在启动时不显示文本

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

    NSString* cellIdentifier = @"cellIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    if(cell == nil) 
    { 
     cell = (UITableViewCell*) [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier]; 

    } 
    cell.accessoryType = UITableViewCellAccessoryNone; 


    if (indexPath.section == 0) { 


     if(indexPath.row == cellPos) 
     { 
      cell.accessoryType = UITableViewCellAccessoryCheckmark; 
      cell.selected = YES; 
     } 
     else 
     { 
      cell.accessoryType = UITableViewCellAccessoryNone; 
      cell.selected = NO; 
     } 



     switch (indexPath.row) { 
      case 0: 
       cell.textLabel.text = @"English"; 
       cell.imageView.image = [UIImage imageNamed:@"english.png"]; 
       break; 
      case 1: 
       cell.textLabel.text = @"Español"; 
       cell.imageView.image = [UIImage imageNamed:@"spanish.png"]; 
       break; 
      case 2: 
       cell.textLabel.text = @"Deutsch"; 
       cell.imageView.image = [UIImage imageNamed:@"germany.png"]; 
       break; 
      case 3: 
       cell.textLabel.text = @"Français"; 
       cell.imageView.image = [UIImage imageNamed:@"french.png"]; 
       break; 
      case 4: 
       cell.textLabel.text = @"Italiano"; 
       cell.imageView.image = [UIImage imageNamed:@"italian.png"]; 
       break; 
      default: 
      break;} 
    } 
    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
    cellPos = indexPath.row; 
    [tableView reloadData]; 
} 
+0

static NSString * cellIdentifier = @“cellIdentifier”; –

+0

你在哪里设置cellPos? – MishieMoo

+0

设置为.h并在viewDidLoad指定值时 – Ruth85

回答

0

您是否试过在开关块后移动if-else块?这样,您可以在将单元格设置为选中状态之前设置单元格的文本。它只是第一次出现这一事实表明,这可能是一个操作秩序的问题。

+0

是的,我做过,但在这种情况下,您会看到一个黑色填充的正方形而不是文本。也只在首次调用默认选定单元时。 – Ruth85

+0

尝试添加'cell.textLabel.backgroundColor = [UIColor clearColor];'到您实例化单元格的if块。 – glorifiedHacker

+0

嘿!那太棒了!工作,谢谢! – Ruth85

相关问题