我使用 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
。最后将删除代码包裹在beginUpdates
和endUpdates
关于ios - 删除 UITableViewCell 时出现 NSException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47862811/