ios - 删除 UITableViewCell 时出现 NSException

标签 ios swift uitableview lldb nsexception

我使用 Github 上的开源库 MGSwipeTableCell ( https://github.com/MortimerGoro/MGSwipeTableCell ) 在滑动表格 View 的单元格时显示按钮。

当我点击通过滑动显示的按钮时发生崩溃。我在这里定义了点击按钮时应该调用的闭包:

// This is in the library delegate method:
// func swipeTableCell(_ cell: MGSwipeTableCell, swipeButtonsFor direction: MGSwipeDirection, swipeSettings: MGSwipeSettings, expansionSettings: MGSwipeExpansionSettings) -> [UIView]?

// local variables: buttons, one of which is:
let rejectFriendReqButton = MGSwipeButton(title: "", icon: UIImage(named: "X"), backgroundColor: RED) { (cell) -> Bool in

    DispatchQueue.main.async(execute: {
        self.friendListTableView.deleteRows(at: [self.friendListTableView.indexPath(for: cell)!], with: .fade)
    })

    return FriendingCloudKitUtils.declineFriendRequest()
}

// In an if-else ladder:
else if direction == MGSwipeDirection.rightToLeft { // reject friend request

    if section == 2 { // friend requests

        if direction == MGSwipeDirection.leftToRight { // accept friend request
            return [acceptFriendReqButton]
        }

        else if direction == MGSwipeDirection.rightToLeft { // reject friend request

            return [rejectFriendReqButton]
        }
    }
    else if self.friendListTableView.indexPath(for: cell)?.section == 4 { // in friend section
        return [unfriendButton]
    }
}

崩溃发生在我调用 deleteRows 的行。我得到一个 NSException。当我在 lldb 中执行 po $arg1 时,我得到:

error: Couldn't materialize: couldn't read the value of register x0
error: errored out in DoExecute, couldn't PrepareToExecuteJITExpression

我已经尝试了更多可能的解决方案,超出了我的追踪范围,其中包括将按钮存储为全局变量而不是本地变量。

其他可能相关的注释:

当我进入 Debug模式时,该表确实存在:

<UITableView: 0x10186c000; frame = (0 0; 375 554); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x17425a760>; layer = <CALayer: 0x174236760>; contentOffset: {0, 0}; contentSize: {375, 357.5}>

正在解包的 indexPath 也不为 nil,并且具有正确的节号和行号:

lldb) po self.friendListTableView.indexPath(for: cell)!
▿ 2 elements
  - 0 : 2
  - 1 : 0

关于导致此 NSException 的原因以及我如何解决它的任何想法?

最佳答案

    self.friendListTableView.beginUpdates()
    your_dataSource.remove(at: self.friendListTableView.indexPath(for: cell)!)
    self.friendListTableView.deleteRows(at: [self.friendListTableView.indexPath(for: cell)!])
    self.tableView.endUpdates()

为了用动画从tableView中删除行,先修改数据源,然后调用deleteRows。最后将删除代码包裹在beginUpdatesendUpdates

关于ios - 删除 UITableViewCell 时出现 NSException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47862811/

相关文章:

ios - 如何强制执行核心数据关系的最大计数?

ios - 两行提示 - Swift

ios - CloudKit 订阅不起作用

ios - 使用 icloud 进行本地核心数据存储

ios - 使用 Storyboard的 swift 2.0 中的通用 Controller

ios - 根据内容以编程方式自动调整 UITableViewCell 的大小

ios - 如何在 UITableview 中创建一个包含 n 行的 UITextField

uitableview - 在哪里可以找到 Xamarin 的 TableView Intent 的 UX 文档?

iOS 自定义 View 不会立即显示图像

ios - Swift 3 录制音频并上传到 Firebase 存储和播放