且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在UITableView中删除多个(尚未加载的)行

更新时间:2023-02-06 23:05:10

- (void)tableView:(UITableView *)tableView 
            commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
            forRowAtIndexPath:(NSIndexPath *)indexPath {
int row = indexPath.row;


if(indexPath.section ==0) {
    // Need to remove both the Car and the Models associated with it.
    // Begin updates. Doesn't commit edits till endUpdates;
    [tableView beginUpdates];

    // Get the indexPaths to be removed.
    // Cars is straight forward.
    NSMutableArray *carsIndexPath = [[NSMutableArray alloc] init];
    NSIndexPath *carsIndexPathObj = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
    [carsIndexPath addObject:carsIndexPathObj];

    // Manually make index paths for models associated with the car.
    NSMutableArray *modelIndexPaths = [self getIndexPaths:indexPath.row];

    // Now remove from model
    [models removeObjectAtIndex:indexPath.row];
    [cars removeObjectAtIndex:indexPath.row];

    // Remove from Table
    [tableView deleteRowsAtIndexPaths:carsIndexPaths withRowAnimation:UITableViewRowAnimationLeft];
    [tableView deleteRowsAtIndexPaths:modelIndexPaths withRowAnimation:UITableViewRowAnimationLeft];

    // Commit updates
    [tableView endUpdates];

    // Reload data.
    [tableView reloadData];

}
}

-(NSMutableArray *) getIndexPaths:(NSInteger) index {   
    NSMutableArray *indexPathArray = [[NSMutableArray alloc] init];
    int offset = 0;
    for (int i=0; i<index; i++) {
        NSMutableArray *tempArr = [models objectAtIndex:i];
        offset += [tempArr count];
    }
    NSMutableArray *currentModels = [models objectAtIndex:index];
    for (int i=0; i<[currentModels count]; i++) {
        NSIndexPath *indexPathTemp = [NSIndexPath indexPathForRow:offset+i inSection:1];
        [indexPathArray addObject:indexPathTemp];
    }   
    return indexPathArray;
}

(如果这有任何意义)