UITableView tap to deselect cell

前端 未结 14 1732
野性不改
野性不改 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:35

    You can try this one, it's works for me (for single selection or multiple selections):

    A. allocate a private NSMutableArray in viewDidLoad like:

    @interface XXXViewController : UIViewController 
    {
        NSMutableArray * selectedZoneCellIndexPaths;
    }
    
    
    - (void)viewDidLoad
    {
        selectedZoneCellIndexPaths = [[NSMutableArray alloc] init];
    }
    

    B. in UITableViewDelegate didSelectRow add following lines

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
         for (NSIndexPath * iter in selectedZoneCellIndexPaths)
         {
             if (indexPath.section == iter.section && indexPath.row == iter.row)
             {
                 [tableView deselectRowAtIndexPath:indexPath animated:YES];
                 [selectedZoneCellIndexPaths removeObject:iter];
                 return;
             }
         }
         [selectedZoneCellIndexPaths addObject:indexPath];
         return;
    }
    

提交回复
热议问题