2012-01-15 7 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]; 
} 

...とにかく私はUITableViewCellAccessoryNoneに戻ってそれを復元することができないんだUITableViewCellAccessoryCheckmarkとしてスタイルが設定されたら! 私も呼び出そう:

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

が、チェックマークを削除しません...私は何をすべきでしょうか?

EDIT:実装はOKですが、問題は、カスタムUITableViewCellのサブクラスにあった...申し訳ありません:P

答えて

0

再びセルをタップするには、セルではなく選択解除を選択することに相当します。

を確認するには、- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPathメソッドで切り替えが必要です。

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' :) –

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 
    } 
} 
関連する問題