• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 从 uitableview cellForRowAtIndexPath 委托(delegate)中删除行时应用程序崩溃

[复制链接]
菜鸟教程小白 发表于 2022-12-13 10:53:47 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有一个表格 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap