UITableViewCell Accessory Type Checked on Tap & Set other unchecked

后端 未结 9 2245
小蘑菇
小蘑菇 2020-12-03 03:02

I am confused a little bit about settings table view cell accessories.

I have fixed two sections in my table

  • Home
  • Office

What I

9条回答
  •  [愿得一人]
    2020-12-03 03:27

    Here's how I do it with static cells, and bypassing cellForRow

    Create a var that stores the location of the "checked" cell.

     NSInteger _checkedCell;
    

    Then just implement willDisplayCell and didSelectRow methods like this.

    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
        if (_checkedCell == indexPath.row) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
        else {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        _checkedCell = indexPath.row;
        [self.tableView reloadData];
    }
    

提交回复
热议问题