2017-12-18 266 views
0

我正在使用Github上的开源库MGSwipeTableCellhttps://github.com/MortimerGoro/MGSwipeTableCell)用于在刷表的视图的单元格时显示按钮。NSException当删除UITableViewCell

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

// 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任何想法和我怎么可能解决呢?

回答

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() 

为了从带有动画的tableView中删除该行,首先修改数据源,然后调用deleteRows。如果你想删除的行只是单纯地找到该行的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) 
    } 
} 

这将处理您删除行操作最后包裹在beginUpdatesendUpdates

0

删除代码。

+0

我不认为这是图书馆的必要条件。该演示不使用典型的表视图委托方法:https://github.com/MortimerGoro/MGSwipeTableCell/blob/master/demo/MailAppDemoSwift/MailAppDemoSwift/MailViewController.swift – mlecoz