在UITableView中删除多个(尚未加载的)行 [英] Deleting multiple (not yet loaded) rows in UITableView

查看:103
本文介绍了在UITableView中删除多个(尚未加载的)行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我的设置如下 -

tableview中有两个部分,第1部分中的每个元素都与第二部分中的多个元素相关联。

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)

第1节


  • 宝马

  • Acura

  • Merc

第2节


  • 328i

  • 335i

  • RX

  • LX

  • TX

  • C300

  • C550

  • 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"

Models []中的每个元素都是一个向量,它们的下面列出了

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

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

所以对于我正在尝试构建的功能。如果用户单击删除并尝试删除BMW,则应用程序需要从第1部分中删除BMW的条目,并在第2部分中删除328i和335i的条目。但是,用户可以独立删除第二部分的任何一行。

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?

推荐答案

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

(如果这有任何意义)

这篇关于在UITableView中删除多个(尚未加载的)行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆