我使用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
}
}
}
任何人都有如何避免这个问题的建议?
唉唉。所以我删除了“!”我不再崩溃。但是,当我拖动到视图的顶部时,我的单元格/快照被禁用。它似乎在其他单元格上盘旋。 – booklynios
在else语句中,您将不得不管理如何处理这些特定边界情况。例如,如果它结束在屏幕的顶部,可以将它插入索引0处。如果它位于底部,则将其插入到表格的末尾。 – 72A12F4E
你在说什么snapshot.layer.edgeAntialiasingMask?抱歉。我只有一年多的时间开发。 – booklynios