1

2 つのセクション/セクション ヘッダーを持つテーブル ビューの各セルに UILongPressGestureRecognizer を使用しています。最初のセクションのセルを長押ししてテーブル ビューの上部にドラッグすると、アプリがクラッシュします。(2 番目のセクションでは longPressGestureRecognizer を無効にしました)。以下の行で例外ポイントを取得します。

var indexPath: NSIndexPath? = self.routeTableView.indexPathForRowAtPoint(場所)!

デバッガーでこのエラーを受け取ります。

致命的なエラー: オプション値のラップ解除中に予期せず 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
            }
        }
   }

この問題を回避する方法について何か提案はありますか?

4

1 に答える 1

0

呼び出すと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 は、uitableview の最初のセルに対してのみ nil を返します

于 2015-07-21T19:46:56.163 に答える