提问者:小点点

使用“插入的”行初始化UITableView


我有一个UITableView,我想在其中插入和删除行。

如果我有一个包含 10 个值的数组,并且从包含所有行的表视图开始,则表显示正常。然后,如果我想过滤掉除[0,1,5]之外的所有内容。然后,我完成删除索引路径处的行的过程:[2,3,4,6,7,8,9],返回的行数为3。我不必更改我的单元格呈现代码,它显示行:[0,1,5]。

但是,如果在过滤完所有内容后,我想返回到这个表,并从persistence中加载包含行[0,1,5]的表,我就有麻烦了。该表加载行数:3,并呈现单元格:[0,1,2]。

我尝试用0行初始化viewDidLoad中的表,然后在viewDid众目睽睽中调用插入行:[0,1,5]-但这会引发异常:

*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2380.17/UITableView.m:909

而且我已经验证了在viewDidLoad中,打印的行数是0,而就在抛出这个错误之前,打印的行数是3。

如果我改为插入[0,1,2]则进行编辑-这有效...但是再一次...我最终需要[0,1,5]出现。

 - (void) viewDidAppear:(BOOL) animated
 {
    [super viewDidAppear:animated];

    NSArray *initializing_indeces = @[[NSIndexPath indexPathForRow:0 inSection:0], [NSIndexPath indexPathForRow:1 inSection:0], [NSIndexPath indexPathForRow:5 inSection:0]];
    [self.userDataTable insertRowsAtIndexPaths:initializing_indeces withRowAnimation:UITableViewRowAnimationAutomatic];
}
- (int) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Using different booleans, but the gist:
    if (viewDidLoad)
        return 0;
    if (viewDidAppear || filtered)
        return 3;
    if (expanded)
        return 10;
}
- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];

    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELL"];
    }


    [cell.textLabel setText:[NSString stringWithFormat:@"%i",indexPath.row]];

    return cell;
}

共3个答案

匿名用户

我必须自己跟踪地图:

- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];

    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELL"];
    }


    int row = indexPath.row;
    if (filtered) {
        // Translate 0 - 0
        // Translate 1 - 1
        // Translate 2 - 5
    }

    [cell.textLabel setText:[NSString stringWithFormat:@"%i", row]];

    return cell;
}

我最后使用了一个NSMutableArray,过滤后它变成了[0,1,5],所以访问这个数组的索引indexPath.row of 2返回5。

匿名用户

@鱼stix

[单元格文本标签集文本:[NS字符串使用格式:@“%i”,索引路径.row]];

代码中的这个方法清楚地表明,文本将是带有indexPath的NSString。行的值。现在,当您删除行和数据源时,从10=[0,1,5][2,3,4,6,7,8,9]3=[0、1,5]。您的数据源已减少为计数为3的数组。因此,您将只获得三行作为返回的bbynumberOfRowsInSection方法。这三行将具有这些indexPath。行的0、1、2。因此,您得到的是0,1,2,而不是0,1,5

如果在< code > cellforrowatdinexpath 方法中打印indexPath(过滤后),您将看到它们分别为[0,0] [0,1] [0,2],表示第0节和第0、1、2行。因此你的结果

如在得到你想要的结果。这样做。采取属性@property(保留,非原子)NS阵列*数据阵列;

在viewDidLoad:self中。dataArray=[NSArray阵列WithObjects:@“0”,@“1”,@”2“,@”3“,@“4”,@(5),@(6),@”7“,@(8),@“9”,nil];

在numberOfRowsInSection中返回[array count];

在cellForRow中[cell.textLabel setText:[self.dataArray objectAtIndex:indexPath.row]];

现在只需在viewWillAppear或任何你想要的地方将dataSource(我指的是dataArray)更改为过滤或扩展状态,并使用[self.tableView reloadData]重新加载表数据;

希望我是清楚的。干杯,玩得开心。

匿名用户

首先,在您的案例中numberOfRowsInSectionreturnNSInteger

我不知道什么是重生。我假设该问题必须连接到代码中的此部分,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

通常,您在这些数据源方法之一中犯了错误。也许不可能说出到底出了什么问题,但它可能是这样的:你正在告诉表格视图的编号的行数(在你的例子中是10),而在单元格中,你只处理10 - 1行。如果您向我们展示您的数据源的实现,则告诉发生了什么会容易得多。