How to uncheck all rows using UITableViewCellAccessoryCheckmark

后端 未结 4 1311
栀梦
栀梦 2021-02-20 10:48

I\'ve got a UITableView with each row containing a checkbox using UITableViewCellAccessoryCheckmark. I can\'t figure out how to uncheck all the checkbo

4条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-20 11:35

    You're probably setting some kind of property with this method. So what i do is:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // 1. first unsetting the property
        [object someProperty:nil];
    
        // 2. call the reloadData method to uncheck all the checkmarks
        [tableView reloadData];
    
        // 3. check the selected cell
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    
        // 4. set the checked property
        [object setSomeProperty:[indexpath row]];
    }
    

    And in my cellForRowAtIndexPath methods i got something like the following code:

        if([object someProperty] == [indexpath row]){
            [cell setAccessoryType:UITableViewCellAccessoryCheckmark];        
        } else {
            [cell setAccessoryType:UITableViewCellAccessoryNone];
        }
    

提交回复
热议问题