代码之家  ›  专栏  ›  技术社区  ›  Tejaswi Yerukalapudi

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

  •  0
  • Tejaswi Yerukalapudi  · 技术社区  · 14 年前

    我在尝试从UITableview中删除尚未加载(或不可见)的行时遇到了一些问题。

    我的设置如下-

    给你举个例子(数据与我想做的并不相关,但我相信这将是一个不需要太多解释的例子)

    • 宝马
    • 默克

    • 328i号
    • 335i号
    • 接收
    • 勒克斯
    • 德克萨斯州
    • C550型

    我的内部模型是这样的-

    NSMutableArray Cars[]
    NSMutableArray Models[]
    
    cars[0] = "BMW"
    cars[1] = "Acura"
    cars[2] = "Merc"
    

    模型[]中的每个元素都是一个向量,其内容如下所示

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

    所以对于我尝试构建的功能。如果用户点击删除并试图删除宝马,应用程序需要删除宝马的条目从第1节和328i和335i的条目在第二节。但是,用户可以独立地删除第二节的任何一行。

    2 回复  |  直到 9 年前
        1
  •  1
  •   Reena    14 年前

    nsmutablerarray*Cars=[[nsmutablerarray 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];
    

    如果删除BMW,则从Models数组中删除第一个元素,从Cars数组和reload表中删除第一个元素。 即

    [Cars removeObjectAtIndex:0];
    [Models removeObjectAtIndex:0];
    [tableview reload];     //tableview - object of UiTableView
    
        2
  •  0
  •   Tejaswi Yerukalapudi    14 年前
    - (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;
    }