我正在使用Github MGSwipeTableCell
(https://github.com/MortimerGoro/MGSwipeTableCell)上的开源库来滑动表格视图的单元格时显示按钮。
当我点击通过滑动显示的按钮时发生崩溃。我定义了在点击按钮时应该调用的闭包:
// 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
我尝试了比我能追踪的更多可能的解决方案,其中将按钮存储为全局变量而不是本地变量。
其他可能相关的说明:
当我进入调试模式时,表确实存在:
<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
也不是零并且具有正确的部分和行号:
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
中
如果要删除行,只需查找该行的indexPath值并调用此方法即可
func tableView(_ tableView: UITableView, commit editingStyle:
UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
print("Deleted")
your_dataSource.remove(at: indexPath.row)
self.tableView.deleteRows(at: [indexPath], with: .automatic)
}
}
它将处理您的删除行操作。