How to properly toggle UITableViewCell's accesoryType on cell selection/deselection?

前端 未结 4 1744
野性不改
野性不改 2021-01-03 06:08

I\'m trying to toggle accesoryType when a table cell is selected/deselected... the behavior should be: tap -> set accessoryType to UITableViewCellAc

4条回答
  •  遥遥无期
    2021-01-03 06:39

    - (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
        }
    }
    

提交回复
热议问题