UITableView: can I delete multiple rows?

前端 未结 2 1211
滥情空心
滥情空心 2021-01-28 23:11

I want to delete multiple rows from a table view, based on the user\'s selection. Obviously I can\'t use the didSelectRowAtIndexPath method because it will be calle

2条回答
  •  北荒
    北荒 (楼主)
    2021-01-28 23:39

    You can do something this way:

    - (void)tableView:(UITableView *)theTableView
          didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath {
    
    [theTableView deselectRowAtIndexPath:[theTableView indexPathForSelectedRow] animated:NO];
    UITableViewCell *cell = [theTableView cellForRowAtIndexPath:newIndexPath];
    if (cell.accessoryType == UITableViewCellAccessoryNone) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        [selectedCellsMutableArray addObject:newIndexPath];
    } else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
        cell.accessoryType = UITableViewCellAccessoryNone;
        [selectedCellsMutableArray removeObjectIdenticalTo:newIndexPath];
    }
    

    }

    When user press Delete Selected button - just invoke something like

    // change your model here and then:
    [yourView deleteRowsAtIndexPaths:selectedCellsMutableArray
                    withRowAnimation:UITableViewRowAnimationRight];
    

提交回复
热议问题