2011-08-24 59 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

相關問題