开发者

Deleting multiple (not yet loaded) rows in UITableView

I'm having a bit of trouble trying to delete rows that haven't been loaded (or not visible) from a UITableview.

My setup is as follows -

There are two sections in the tableview and each element in section 1 is associated with multiple elements from section two.

To give you an example (The data isn't really related to what I'm trying to do, but I believe this will be an example that doesn't really require much explanation开发者_运维问答)

Section 1

  • BMW
  • Acura
  • Merc

Section 2

  • 328i
  • 335i
  • RX
  • LX
  • TX
  • C300
  • C550

My internal model goes something like this -

NSMutableArray Cars[]
NSMutableArray Models[]

cars[0] = "BMW"
cars[1] = "Acura"
cars[2] = "Merc"

Each element in Models[] is a vector and their conents are listed below

Models = [ ["328i", "335i"], ["RX", "LX", "TX"], ["C300", "C550"] ];

So for the functionality I'm trying to build. If the user clicks delete and tries to delete BMW, the App needs to remove the entry for BMW from section 1 and the entries for 328i and 335i in section two. However, the user is free to delete any single row of section two independently.

Can anyone point me to a way I can proceed with this?


NSMutableArray *Cars = [[NSMutableArray alloc] initWithObjects:@"BMW",@"Acura",@"Merc",nil];

NSMutableArray *Arr1 = [[NSMutableArray alloc]initWithObjects:@"328i", @"335i",nil];
NSMutableArray *Arr2 = [[NSMutableArray alloc]initWithObjects:@"RX", @"LX", @"TX",nil];
NSMutableArray *Arr3 = [[NSMutableArray alloc]initWithObjects:@"C300", @"C550",nil];

NSMutableArray * Models = [[NSMutableArray alloc] initWithObjects:Arr1,Arr2,Arr3,nil];

On delete if you delete BMW then remove 1st element from Models array and and 1st element from Cars array and reload table. i.e.

[Cars removeObjectAtIndex:0];
[Models removeObjectAtIndex:0];
[tableview reload];     //tableview - object of UiTableView


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

(If this makes any sense at all)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜