2012-01-15 82 views
4

我試圖切換accesoryType當選擇一個表格單元格/取消...的行爲應該是:自來水 - >設置accessoryType到UITableViewCellAccessoryCheckmark - >再次點擊電池 - >回滾到UITableViewCellAccessoryNone類型。 在我的控制器實現如下:如何在單元格選擇/取消選擇時正確切換UITableViewCell的accesoryType?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    [cell setAccessoryType:UITableViewCellAccessoryCheckmark]; 
} 

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    [cell setAccessoryType:UITableViewCellAccessoryNone]; 
} 

...反正一次作爲UITableViewCellAccessoryCheckmark我無法把它還原回UITableViewCellAccessoryNone風格配置! 我也試着撥打:

[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone]; 

,但不刪除對號......我該怎麼辦?

編輯:執行是好的,問題是在自定義的UITableViewCell子類...對不起:P

回答

0

攻細胞再次等同於細胞,而不是取消選擇的選擇。

您需要在- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath方法中進行切換才能檢查cell.accessoryType == UITableViewCellAccessoryCheckmark

13

試試這個,如果這是你想要的

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
     if (cell.accessoryType == UITableViewCellAccessoryCheckmark) 
     { 
      cell.accessoryType = UITableViewCellAccessoryNone; 
     } 
     else 
     { 
      cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     } 
    } 
+1

對於快速和骯髒的:'cell.accessoryType^= UITableViewCellAccessoryCheckmark' :) – 2014-10-25 06:10:26

2

如果你想只有一個行作爲勾選使用本

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    cell.accessoryType = (cell.accessoryType == UITableViewCellAccessoryCheckmark) ? UITableViewCellAccessoryNone : UITableViewCellAccessoryCheckmark; 
    if (_lastSelectedIndexPath != nil) 
    { 
     UITableViewCell *lastSelectedCell = [tableView cellForRowAtIndexPath:_lastSelectedIndexPath]; 
     lastSelectedCell.accessoryType = UITableViewCellAccessoryNone; 
    } 
    _lastSelectedIndexPath = indexPath; 
} 
0
- (void)tableView:(UITableView *)theTableView didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath { 
    [theTableView deselectRowAtIndexPath:[theTableView indexPathForSelectedRow] animated:NO]; 
    UITableViewCell *cell = [theTableView cellForRowAtIndexPath:newIndexPath]; 
    if (cell.accessoryType == UITableViewCellAccessoryNone) { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     // Reflect selection in data model 
    } else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
     // Reflect deselection in data model 
    } 
} 
相關問題