2015-07-21 84 views
1

我使用UILongPressGestureRecognizer爲表格視圖中的每個單元具有兩個節/節標題。當我在第一部分中對單元格進行長按並拖動到表格視圖頂部時,我的應用程序崩潰。 (我爲第二部分禁用了longPressGestureRecognizer)。我在下面一行得到一個異常點,UILongPressGestureRecognizer在tableview中被拖動到視圖頂部時崩潰 - SWIFT

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

我收到此錯誤調試器,

致命錯誤:意外發現零而展開的可選值

我的代碼如下,

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 
      } 
     } 
    } 

任何人都有如何避免這個問題的建議?

回答

0

當您致電self.routeTableView.indexPathForRowAtPoint(location)!時,您會強制解開返回的值,在這種情況下,它將爲零。這是你崩潰的地方。

您的代碼應該做這樣的事情:

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

+0

唉唉。所以我刪除了「!」我不再崩潰。但是,當我拖動到視圖的頂部時,我的單元格/快照被禁用。它似乎在其他單元格上盤旋。 – booklynios

+0

在else語句中,您將不得不管理如何處理這些特定邊界情況。例如,如果它結束在屏幕的頂部,可以將它插入索引0處。如果它位於底部,則將其插入到表格的末尾。 – 72A12F4E

+0

你在說什麼snapshot.layer.edgeAntialiasingMask?抱歉。我只有一年多的時間開發。 – booklynios