【问题标题】:NSException when deleting UITableViewCell删除 UITableViewCell 时出现 NSException
【发布时间】:2017-12-18 05:53:50
【问题描述】:

我正在使用 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 也不是 nil 并且具有正确的部分和行号:

lldb) po self.friendListTableView.indexPath(for: cell)!
▿ 2 elements
  - 0 : 2
  - 1 : 0

关于导致此NSException 的原因以及我如何解决它的任何想法?

【问题讨论】:

    标签: ios swift uitableview lldb 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。最后将删除代码包裹在beginUpdatesendUpdates

    【讨论】:

      【解决方案2】:

      如果要删除行,只需找到该行的 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)
        }
       }
      

      它将处理您的删除行操作。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-21
      • 2015-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多