ios - UILongPressGestureRecognizer crashes in tableview when dragged to top of view - SWIFT -


i using uilongpressgesturerecognizer each cell in table view has 2 sections/section headers. when long press on cell in first section , drag top of table view app crashes. (i have disabled longpressgesturerecognizer second section). exception point @ line below,

var indexpath: nsindexpath? = self.routetableview.indexpathforrowatpoint(location)!

i receive error in debugger,

fatal error: unexpectedly found nil while unwrapping optional value

my code below,

func longpressgesturerecognized(gesture: 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 snapshot of selected row using helper method                 snapshot = customsnapshotfromview(cell)                  //add snapshot subview, centered @ 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 , different form source?                 if indexpath != sourceindexpath{                     //update data source                     self.messageoptions.exchangeobjectatindex(indexpath!.row, withobjectatindex: sourceindexpath!.row)                     //move row                     self.routetableview.moverowatindexpath(sourceindexpath!, toindexpath: indexpath!)                     //and update source in sync ui changes                     sourceindexpath = indexpath                 }              default:                 //clean                 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             }         }    } 

anyone have suggestions on how avoid issue?

when call self.routetableview.indexpathforrowatpoint(location)! force unwrapping value comes back, , in case, nil. crashing.

your code should this:

if let indexpath = self.routetableview.indexpathforrowatpoint(location) {   //do normal code } else {   //the point didn't map cell! maybe on header or footer?    //handle case differently. } 

also, found answer might help:

indexpathforrowatpoint returns nil first cell in uitableview


Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -