52

「オン」のチェックマークを「オフ」に変更するには、とのCellAccessoryType間を変更する必要があると考えてよろしいですか?nonecheckmarkdidSelectRowAtIndexPath

私はこれを行ったが、動作がiPhoneの自動ロック設定のチェックマークセルのように完全に同一ではないことに気付いた.

または、チェックマークを処理するための他の方法はありますか?

4

8 に答える 8

81
  1. selectedRowテーブル セクションでチェックされた項目を表す行のインデックスを表すというビュー コントローラーのプロパティを保持します。

  2. ビュー コントローラーの-tableView:cellForRowAtIndexPath:デリゲート メソッドで、セルの値が等しい場合accessoryTypeにを設定します。それ以外の場合は、に設定します。cellUITableViewCellAccessoryCheckmarkindexPath.rowselectedRowUITableViewCellAccessoryNone

  3. ビュー コントローラの-tableView:didSelectRowAtIndexPath:デリゲート メソッドで、selectedRow値をindexPath.row選択されているものに設定します。次に例を示します。self.selectedRow = indexPath.row

于 2010-05-09T09:51:47.843 に答える
61

別の解決策:

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSIndexPath *oldIndex = [self.tableView indexPathForSelectedRow];
    [self.tableView cellForRowAtIndexPath:oldIndex].accessoryType = UITableViewCellAccessoryNone;
    [self.tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
    return indexPath;
}

そしてええ:そうかどうかを確認する必要はありませoldIndexnil:)


Swift 3 以降:

override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
    if let oldIndex = tableView.indexPathForSelectedRow {
        tableView.cellForRow(at: oldIndex)?.accessoryType = .none
    }
    tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark

    return indexPath
}
于 2013-04-06T08:38:57.450 に答える
5

迅速:

var selectedCell:UITableViewCell?{
    willSet{
        selectedCell?.accessoryType = .None
        newValue?.accessoryType = .Checkmark
    }
}

それから:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    selectedCell = self.tableView.cellForRowAtIndexPath(indexPath)
    self.mapView.selectAnnotation(self.mapView.annotations[indexPath.row], animated: true)
}
于 2016-01-30T23:01:12.537 に答える
3

そのはず

didHighlightRowAtIndexPath

それ以外の

tableView:didSelectRowAtIndexPath

于 2012-11-30T13:17:20.657 に答える
3

アレックスの答えは、 reload table を .hに追加した後にのみ機能しました

{
  int selectedCell;
}
@property(nonatomic,readwrite)int selectedCell;

in .m * cellForRowAtIndexPath *

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

そしてどこでも didHighlightRowAtIndexPath または didSelectRowAtIndexPath

 self.selectedCell = indexPath.row;
    [self.tableView reloadData]; 
于 2013-02-25T09:45:31.123 に答える