时间:2010-07-23 19:22:53

标签: iphone uitableview

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

- (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;
}

(如果这有任何意义的话)