OGeek|极客世界-中国程序员成长平台

标题: ios - 从 uitableview cellForRowAtIndexPath 委托(delegate)中删除行时应用程序崩溃 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 10:53
标题: ios - 从 uitableview cellForRowAtIndexPath 委托(delegate)中删除行时应用程序崩溃

我有一个表格 View 。我正在实现一个功能,其中单元格将开始褪色(在 30 秒的时间内减少 alpha)。完成 30 秒后, View 动画的完成处理程序被调用以从数据源中永久删除行(一个数组)。所有的东西都在 cellForRowAtIndexPath 委托(delegate)方法中。 我的问题是在更新前和更新后的数组计数之间保持同步。

代码:

- (UITableViewCell *)tableViewUITableView *)tableView 
     cellForRowAtIndexPathNSIndexPath *)indexPath {

    static NSString *postCellId = @"postCell";
    UITableViewCell *cell = nil;

    NSUInteger row = [indexPath row];
    cell = [tableView dequeueReusableCellWithIdentifier:postCellId];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc]
             initWithStyle:UITableViewCellStyleSubtitle
             reuseIdentifier:postCellId] autorelease];
    }

    Post *currentPost = [posts objectAtIndex:row];
    cell.textLabel.text = [currentPost postTitle];
    cell.textLabel.font = [UIFont systemFontOfSize:14];

    cell.detailTextLabel.text = [currentPost postDescr];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:10];

    NSTimeInterval duration = 30;
    [UIView animateWithDuration:duration delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionAllowUserInteraction
                 animations:^ {
         cell.contentView.alpha = 0;
    } completion:^(BOOL finished) {
        [self.tableView beginUpdates];
        NSLog(@"delete index >> %d from array >> %@", row, posts);
        [posts removeObjectAtIndex:indexPath.row];
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
        [self.tableView endUpdates];
    }];

    return cell;
}

日志:

2015-06-04 07:22:05.764 Feeder[1177:60b] delete index >> 0 from array

( "", "", "", "", "" ) 2015-06-04 07:22:05.766 Feeder[1177:60b] delete index >> 1 from array >> ( "", "", "", "" ) 2015-06-04 07:22:05.767 Feeder[1177:60b] delete index >> 2 from array >> ( "", "", "" ) 2015-06-04 07:22:05.768 Feeder[1177:60b] delete index >> 3 from array >> ( "", "" )

如果你看到最后的日志,那就是崩溃的问题。

崩溃日志:

2015-06-04 07:22:05.770 Feeder[1177:60b] * Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM removeObjectAtIndex:]: index 3 beyond bounds [0 .. 1]'

如何解决它。



Best Answer-推荐答案


您的错误是您将旧的 indexPath 保留在 block 中,而不是更新它。

例如: 如果您的数组中有 2 条记录,则您在完整 block 中的操作是

  1. 删除第 0 行
  2. 删除第 1 行

但是,如果你删除第一个单元格(第 0 行),你的第 1 行 indexPath.row 会更新为 0。但是你想删除 1。

所以,我认为你可以动态获取 indexPath,然后删除

[self.tableView beginUpdates];
NSIndexPath *path = [tableView indexPathForCell:cell];
NSLog(@"delete index >> %d from array >> %@", path.row, posts);
[posts removeObjectAtIndex:path.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:path] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];

我不确定这是否可行,请尝试。

关于ios - 从 uitableview cellForRowAtIndexPath 委托(delegate)中删除行时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30633807/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4