2017-12-18 29 views
0

テーブルビューのセルをスワイプするときに、Github MGSwipeTableCellhttps://github.com/MortimerGoro/MGSwipeTableCell)のオープンソースライブラリを公開しています。UITableViewCellを削除するとNSExceptionが発生する

スワイプで公開されたボタンをタップするとクラッシュする。私はここでボタンをタップしたときに呼び出されるべきでクロージャを定義します。

// 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がnilでないと正しいセクションがあり、行番号:

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

関連する問題