UITable查看外部拖放 Table = 崩溃

UITableView Drag & Drop Outside Table = Crash

好的

我的拖放功能几乎可以完美运行。我长按一个单元格,它可以让我顺利地将按下的单元格移动到其他两个单元格之间的新位置。 table 调整并将更改保存到核心数据。太棒了!

坏人

我的问题是,如果我将单元格拖到 table 中底部单元格的下方,即使我不松开(松开)单元格...应用程序也会崩溃。如果我缓慢地拖动,当单元格穿过最后一个单元格的 y 中心时它真的会崩溃......所以我认为这是一个与快照获取位置相关的问题。不太重要但可能相关的是,如果我长按最后一个包含值的单元格下方,它也会崩溃。

drag/drop 运行一个 switch 语句,该语句根据状态运行三组代码之一:

我的代码改编自本教程:

Drag & Drop Tutorial

我的代码:

 func longPressGestureRecognized(gestureRecognizer: UIGestureRecognizer) {

    let longPress = gestureRecognizer as! UILongPressGestureRecognizer
    let state = longPress.state

    var locationInView = longPress.locationInView(tableView)
    var indexPath = tableView.indexPathForRowAtPoint(locationInView)

    struct My {
        static var cellSnapshot : UIView? = nil
    }
    struct Path {
        static var initialIndexPath : NSIndexPath? = nil
    }

    let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as! CustomTableViewCell;

    var dragCellName = currentCell.nameLabel!.text
    var dragCellDesc = currentCell.descLabel.text


    //Steps to take a cell snapshot. Function to be called in switch statement
    func snapshotOfCell(inputView: UIView) -> UIView {
        UIGraphicsBeginImageContextWithOptions(inputView.bounds.size, false, 0.0)
        inputView.layer.renderInContext(UIGraphicsGetCurrentContext())
        let image = UIGraphicsGetImageFromCurrentImageContext() as UIImage
        UIGraphicsEndImageContext()
        let cellSnapshot : UIView = UIImageView(image: image)
        cellSnapshot.layer.masksToBounds = false
        cellSnapshot.layer.cornerRadius = 0.0
        cellSnapshot.layer.shadowOffset = CGSizeMake(-5.0, 0.0)
        cellSnapshot.layer.shadowRadius = 5.0
        cellSnapshot.layer.shadowOpacity = 0.4
        return cellSnapshot
    }


    switch state {
        case UIGestureRecognizerState.Began:
            //Calls above function to take snapshot of held cell, animate pop out
            //Run when a long-press gesture begins on a cell
            if indexPath != nil && indexPath != nil {
                Path.initialIndexPath = indexPath
                let cell = tableView.cellForRowAtIndexPath(indexPath!) as UITableViewCell!
                My.cellSnapshot  = snapshotOfCell(cell)
                var center = cell.center

                My.cellSnapshot!.center = center
                My.cellSnapshot!.alpha = 0.0

                tableView.addSubview(My.cellSnapshot!)

                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    center.y = locationInView.y

                    My.cellSnapshot!.center = center
                    My.cellSnapshot!.transform = CGAffineTransformMakeScale(1.05, 1.05)
                    My.cellSnapshot!.alpha = 0.98

                    cell.alpha = 0.0

                    }, completion: { (finished) -> Void in

                        if finished {
                            cell.hidden = true
                        }
                })
            }
        case UIGestureRecognizerState.Changed:

            if My.cellSnapshot != nil && indexPath != nil {
                //Runs when the user "lets go" of the cell
                //Sets CG Y-Coordinate of snapshot cell to center of current location in table (snaps into place)
                var center = My.cellSnapshot!.center
                center.y = locationInView.y
                My.cellSnapshot!.center = center

                var appDel: AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
                var context: NSManagedObjectContext = appDel.managedObjectContext!
                var fetchRequest = NSFetchRequest(entityName: currentListEntity)
                let sortDescriptor = NSSortDescriptor(key: "displayOrder", ascending: true )
                fetchRequest.sortDescriptors = [ sortDescriptor ]


                //If the indexPath is not 0 AND is not the same as it began (didn't move)...
                //Update array and table row order
                if ((indexPath != nil) && (indexPath != Path.initialIndexPath)) {

                    swap(&taskList_Cntxt[indexPath!.row], &taskList_Cntxt[Path.initialIndexPath!.row])
                    tableView.moveRowAtIndexPath(Path.initialIndexPath!, toIndexPath: indexPath!)

                    toolBox.updateDisplayOrder()
                    context.save(nil)

                    Path.initialIndexPath = indexPath
                }
            }
        default:
            if My.cellSnapshot != nil && indexPath != nil {
                //Runs continuously while a long press is recognized (I think)
                //Animates cell movement
                //Completion block: 
                //Removes snapshot of cell, cleans everything up
                let cell = tableView.cellForRowAtIndexPath(Path.initialIndexPath!) as UITableViewCell!

                cell.hidden = false
                cell.alpha = 0.0
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    My.cellSnapshot!.center = cell.center
                    My.cellSnapshot!.transform = CGAffineTransformIdentity
                    My.cellSnapshot!.alpha = 0.0
                    cell.alpha = 1.0
                    }, completion: { (finished) -> Void in
                        if finished {
                            Path.initialIndexPath = nil
                            My.cellSnapshot!.removeFromSuperview()
                            My.cellSnapshot = nil
                        }
                })//End of competion block & end of animation

            }//End of 'if nil'

    }//End of switch

}//End of longPressGestureRecognized

潜在罪魁祸首

我的猜测是这个问题与单元格一旦低于最后一个单元格就无法获取坐标有关。它并不是真正浮动的,它不断地设置它相对于其他单元格的位置。我认为解决方案将是一个 if 语句,当没有单元格可供参考时,它会做一些神奇的事情。但是什么!?!由于某些原因,向每个案例添加 nil 检查不起作用。

问题明确

如何避免崩溃并处理我拖动的单元格被拖到最后一个单元格下方的事件?

崩溃截图:

丑女

看来您只需要进行先发制人的检查,以确保您的 indexPath 不为零:

var indexPath = tableView.indexPathForRowAtPoint(locationInView)
if (indexPath != nil) {
    //Move your code to this block
}

希望对您有所帮助!

您没有说明崩溃发生在代码的哪个位置,这使得更难确定发生了什么。在异常上设置断点以确定哪一行是罪魁祸首。为此,请使用 XCode.

中断点列表左下角的“+”

我认为主要问题是 indexPath。有几个问题:

  1. 您正在使用 indexPath,即使它可能为零,在这一行中:

    let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as! CustomTableViewCell;
    
  2. indexPath 可能无效,即使它不是零。检查其部分和行成员是否与 NSNotFound 不同。

最后,我一直在使用预制的开源 UITableView 子类,它可以为您完成所有移动,因此您不必再自己实现它。它还负责自动滚动,您甚至还没有考虑过。直接使用它,或将其用作代码的灵感: https://www.cocoacontrols.com/controls/fmmovetableview