ios - 拖到 View 顶部时,UILongPressGestureRecognizer 在表格 View 中崩溃 - SWIFT

标签 ios swift tableview uilongpressgesturerecogni

我正在为具有两个部分/部分标题的 TableView 中的每个单元格使用 UILongPressGestureRecognizer。当我长按第一部分中的单元格并拖动到表格 View 的顶部时,我的应用程序崩溃了。 (我已经为第二部分禁用了 longPressGestureRecognizer)。我在下面的行中得到一个异常点,

var indexPath: NSIndexPath? = self.routeTableView.indexPathForRowAtPoint(位置)!

我在调试器中收到此错误,

fatal error :在展开可选值时意外发现 nil

我的代码在下面,

func longPressGestureRecognized(手势:UILongPressGestureRecognizer){

    var state: UIGestureRecognizerState = gesture.state

    var location:CGPoint = gesture.locationInView(self.routeTableView)
    var indexPath: NSIndexPath? = self.routeTableView.indexPathForRowAtPoint(location)!

    if indexPath == nil {
        return
    }

    if indexPath?.section != 1 {

            switch(state){
            case UIGestureRecognizerState.Began:
                sourceIndexPath = indexPath

                var cell: UITableViewCell = self.routeTableView .cellForRowAtIndexPath(indexPath!)! as! RouteSelectionCell

                //take a snapshot of the selected row using helper method
                snapshot = customSnapshotFromView(cell)

                //add snapshot as subview, centered at cell's center
                var center: CGPoint = cell.center
                snapshot?.center = center
                snapshot?.alpha  = 0.0
                self.routeTableView.addSubview(snapshot!)
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    center.y = location.y
                    self.snapshot?.center = center
                    self.snapshot?.transform = CGAffineTransformMakeScale(1.05, 1.05)
                    self.snapshot?.alpha = 0.98
                    cell.alpha = 0.0
                    }, completion: { (finished) in
                        cell.hidden = true
                })

            case UIGestureRecognizerState.Changed:
                var center: CGPoint = snapshot!.center
                center.y = location.y
                snapshot?.center = center


                //is destination valid and is it different form source?
                if indexPath != sourceIndexPath{
                    //update data source
                    self.messageOptions.exchangeObjectAtIndex(indexPath!.row, withObjectAtIndex: sourceIndexPath!.row)
                    //move the row
                    self.routeTableView.moveRowAtIndexPath(sourceIndexPath!, toIndexPath: indexPath!)
                    //and update source so it is in sync with UI changes
                    sourceIndexPath = indexPath
                }

            default:
                //clean up
                let cell: UITableViewCell = routeTableView.cellForRowAtIndexPath(sourceIndexPath!)!
                cell.alpha = 0.0
                cell.hidden = false

                UIView.animateWithDuration(0.25, animations: { () -> Void in

                    self.snapshot?.center = cell.center
                    self.snapshot?.transform = CGAffineTransformIdentity
                    self.snapshot?.alpha = 0.0
                    //undo fade out
                    cell.alpha = 1.0

                    }, completion: { (finished) in
                        self.sourceIndexPath = nil
                        self.snapshot?.removeFromSuperview()
                        self.snapshot = nil
                })
                break
            }
        }
   }

有人对如何避免这个问题有任何建议吗?

最佳答案

当您调用 self.routeTableView.indexPathForRowAtPoint(location)! 时,您将强制解包返回的值,在本例中为 nil。这就是你崩溃的地方。

你的代码应该做这样的事情:

if let indexPath = self.routeTableView.indexPathForRowAtPoint(location) {
  //do normal code
} else {
  //the point didn't map back to a cell! Maybe it was on the header or footer? 
  //Handle this case differently.
}

另外,我发现这个答案可能有帮助:

indexPathForRowAtPoint returns nil only for first cell in a uitableview

关于ios - 拖到 View 顶部时,UILongPressGestureRecognizer 在表格 View 中崩溃 - SWIFT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31547677/

相关文章:

swift - 当我从 tableView 中选择一个单元格时,如何使用 navigationController 显示 WebView?

java - TableView:当滚动到达表格底部/顶部时收到通知

ios - 核心数据总是删除第一个对象

ios - ARC 语义问题 : "No visible @interface for UIImageView" and PFImageView Semantic Issues

ios - RxCoreData UI 没有更新

ios - 调试 NSJSONSerialization 内存泄漏

ios - 如何实时更改应用语言?

ios - 如何发布 WatchOS2 应用程序更新?

ios - Swift - 在数组中添加粒度(2 x 2、5 x 5 等)

ios - 从 plist 加载数据到 tableview