I am using the open-source library on Github MGSwipeTableCell
(https://github.com/MortimerGoro/MGSwipeTableCell) for revealing buttons when swiping cells of a table view.
A crash occurs when I tap a button revealed by swiping. I define the closure that should be invoked when tapping the button here:
// 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]
}
}
The crash occurs at the line where I call deleteRows
. I get an NSException
. When I execute po $arg1
in lldb, I get:
error: Couldn't materialize: couldn't read the value of register x0
error: errored out in DoExecute, couldn't PrepareToExecuteJITExpression
I've tried more possible solutions than I can keep track of, among them storing the button as a global variable instead of a local one.
Other potentially relevant notes:
When I go into debug mode, the table does indeed exist:
<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}>
The indexPath
that is being unwrapped also isn't nil and has the correct section and row numbers:
lldb) po self.friendListTableView.indexPath(for: cell)!
▿ 2 elements
- 0 : 2
- 1 : 0
Any ideas on what is causing this NSException
and how I might fix it?
答案 0 :(得分:1)
self.friendListTableView.beginUpdates()
your_dataSource.remove(at: self.friendListTableView.indexPath(for: cell)!)
self.friendListTableView.deleteRows(at: [self.friendListTableView.indexPath(for: cell)!])
self.tableView.endUpdates()
In order to delete the row from tableView with animation, modify the data source first and then call deleteRows
. Finally wrap the delete code in beginUpdates
and endUpdates
答案 1 :(得分:0)
If you want to delete row just simply find the value of indexPath of that row and call this method
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)
}
}
It will handle your delete row operation.