UITableView Drag & Drop Outside Table = Crash

拟墨画扇 提交于 2019-12-05 13:48:24

The Ugly

It seems that you simply need to do a preemptive check, to ensure your indexPath is not nil:

var indexPath = tableView.indexPathForRowAtPoint(locationInView)
if (indexPath != nil) {
    //Move your code to this block
}

Hope that helps!

You don't state where in the code the crash occurs, which makes it harder to determine what is going on. Set a breakpoint on exceptions to determine which line is the culprit. To do that, use the '+' in the bottom-left corner of the breakpoint list in XCode.

The main issue I think is with the indexPath. There are a couple of issues:

  1. You are using the indexPath even though it might be nil, in this line:

    let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as! CustomTableViewCell;
    
  2. The indexPath can be invalid, even though it is not nil. Check for its section and row members to be different from NSNotFound.

Finally, I have been using a pre-made, open source, UITableView subclass that does all the moving for you, so you don't have to implement it yourself anymore. It also takes care of autoscrolling, which you have not even considered yet. Use it directly, or use it as inspiration for your code: https://www.cocoacontrols.com/controls/fmmovetableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!