2011-08-24 104 views
1

以下代碼以空白單元格開頭,並在選定單元格時添加複選標記,但是一旦添加,如果再次按下該單元格,則不會被刪除。每次didSelectRowAtIndexPath被調用時,我都試圖重置它。如何清除細胞以便僅標記一個細胞?謝謝。重置UItableViewCell複選標記

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
     cell.accessoryType = UITableViewCellAccessoryNone; 

    if (indexPath.section == 0) { 

    switch (indexPath.row) { 
     case 0: 
      [email protected]"1"; 
      cell.accessoryType = UITableViewCellAccessoryCheckmark; 
      break; 
     case 1: 
      [email protected]"2"; 
      cell.accessoryType = UITableViewCellAccessoryCheckmark; 
      break; 
     case 2: 
      [email protected]"3"; 
      cell.accessoryType = UITableViewCellAccessoryCheckmark; 
      break; 
     case 3: 
      [email protected]"4"; 
      cell.accessoryType = UITableViewCellAccessoryCheckmark; 
      break; 
     case 4: 
      [email protected]"5"; 
      cell.accessoryType = UITableViewCellAccessoryCheckmark; 
      break; 
     default: 
      break; 
     } 
    } 
}   

回答

3

只要複製粘貼此。有用。

//in your .h file 
    int selectedCell; 

    //in your .m file 
    - (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 == selectedCell) 
      { 
        cell.accessoryType = UITableViewCellAccessoryCheckmark; 
        cell.selected = YES; 
      } 
      else 
      { 
       cell.accessoryType = UITableViewCellAccessoryNone; 
       cell.selected = NO; 
      } 
     } 
    } 



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

嗨全部標記,代碼工作幾近完美節選的是默認選定的單元格不顯示文本,直到另一個單元格被按下!如何解決? – Ruth85

+0

我的意思是,例如,當voidDidLoad,selectedCell = 2和該單元格將不會有文本,直到任何其他單元格被按下。 – Ruth85

+0

的selectedCell的值爲-1。 – mayuur

1

試試這個:

if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { 
    cell.accessoryType = UITableViewCellAccessoryNone; 
} else { 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
} 
0

在你的代碼開始

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    cell.accessoryType = UITableViewCellAccessoryNone; 

從選定單元格刪除檢查標記,但後來你

switch (indexPath.row) { 
    case 0: 
     [email protected]"1"; 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     break; 
    case 1: 
     [email protected]"2"; 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     break; 
    case 2: 
     [email protected]"3"; 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     break; 
    case 3: 
     [email protected]"4"; 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     break; 
    case 4: 
     [email protected]"5"; 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     break; 
    default: 
     break; 
    } 
再次分配勾選

所以,你c一個使用類似

if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { 
    cell.accessoryType = UITableViewCellAccessoryNone; 
} else { 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
} 

來回切換的選中標記,

labelInfo.text = [NSString stringWithFormat:@"%d", indexPath.row + 1]; 

的標籤

0

做到這一點: -

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    { 

     UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 


     if (indexPath.section == 0) { 

      switch (indexPath.row) { 
       case 0: 
        [email protected]"1"; 
        if (cell.accessoryType==UITableViewCellAccessoryCheckmark) 
         cell.accessoryType=UITableViewCellAccessoryNone; 
        else 
         cell.accessoryType = UITableViewCellAccessoryCheckmark; 
        break; 
       case 1: 
        [email protected]"2"; 
if (cell.accessoryType==UITableViewCellAccessoryCheckmark) 
         cell.accessoryType=UITableViewCellAccessoryNone; 
        else 
         cell.accessoryType = UITableViewCellAccessoryCheckmark; 
        break; 
       case 2: 
        [email protected]"3"; 
if (cell.accessoryType==UITableViewCellAccessoryCheckmark) 
         cell.accessoryType=UITableViewCellAccessoryNone; 
        else 
         cell.accessoryType = UITableViewCellAccessoryCheckmark; 
        break; 
       case 3: 
        [email protected]"4"; 
        cell.accessoryType = UITableViewCellAccessoryCheckmark; 
        break; 
       case 4: 
        [email protected]"5"; 
if (cell.accessoryType==UITableViewCellAccessoryCheckmark) 
         cell.accessoryType=UITableViewCellAccessoryNone; 
        else 
         cell.accessoryType = UITableViewCellAccessoryCheckmark; 
        break; 
       default: 
        break; 
      } 
     } 
4

該代碼將刪除表

[[self.tableView visibleCells] makeObjectsPerformSelector:@selector(setAccessoryType:) withObject:UITableViewCellAccessoryNone]; 
+0

我發現UITableViewCellAccessoryNone需要包裝以使其工作:[[self.tableView visibleCells] makeObjectsPerformSelector:@selector(setAccessoryType :) withObject:@(UITableViewCellAccessoryNone)]; – fatuhoku