How to deselect a selected UITableView cell?

前端 未结 24 3172
一整个雨季
一整个雨季 2020-12-07 10:10

I am working on a project on which I have to preselect a particular cell.

I can preselect a cell using -willDisplayCell, but I can\'t deselect it when t

24条回答
  •  隐瞒了意图╮
    2020-12-07 10:33

    Based on saikirans solution, I have written this, which helped me. On the .m file:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
        if(selectedRowIndex && indexPath.row == selectedRowIndex.row) {
    
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
            selectedRowIndex = nil;
    
        }
    
        else {  self.selectedRowIndex = [indexPath retain];   }
    
        [tableView beginUpdates];
        [tableView endUpdates];
    
    }
    

    And on the header file:

    @property (retain, nonatomic) NSIndexPath* selectedRowIndex;
    

    I am not very experienced either, so double check for memory leaks etc.

提交回复
热议问题