UITableView tap to deselect cell

前端 未结 14 1752
野性不改
野性不改 2020-12-16 10:37

I have a UITableViewCell that is selected when tapped. During this selected state, if the user taps the cell again, I want the cell to deselect.

I can\

14条回答
  •  伪装坚强ぢ
    2020-12-16 11:31

    You can actually do this using the delegate method willSelectRowAtIndexPath:

    - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        if ([cell isSelected]) {
            // Deselect manually.
            [tableView.delegate tableView:tableView willDeselectRowAtIndexPath:indexPath];
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
            [tableView.delegate tableView:tableView didDeselectRowAtIndexPath:indexPath];
    
            return nil;
        }
    
        return indexPath;
    }
    

    Note that deselectRowAtIndexPath: won't call the delegate methods automatically, so you need to make those calls manually.

提交回复
热议问题