尝试创建拖放到tableview。迅速

时间:2015-03-08 17:04:48

标签: swift drag-and-drop tableview cell

我一直在尝试使用本指南创建拖放http://www.freshconsulting.com/create-drag-and-drop-uitableview-swift/

这是我在viewdidload中的代码

let longpress = UILongPressGestureRecognizer(target: self, action: "longPressGestureRecognized:")

        longpress.minimumPressDuration = 1.0

        table.addGestureRecognizer(longpress)

和func

   func longPressGestureRecognized(gestureRecognizer: UILongPressGestureRecognizer) {


           func snapshopOfCell(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
        }

        let longPress = gestureRecognizer as UILongPressGestureRecognizer

        let state = longPress.state

        var locationInView = longPress.locationInView(table)

        var indexPath = table.indexPathForRowAtPoint(locationInView)

        struct My {

            static var cellSnapshot : UIView? = nil

        }
        struct Path {

            static var initialIndexPath : NSIndexPath? = nil

        }

        switch state {
        case UIGestureRecognizerState.Began:
            if indexPath != nil {
                Path.initialIndexPath = indexPath
                let cell = table.cellForRowAtIndexPath(indexPath!) as UITableViewCell!
                My.cellSnapshot  = snapshopOfCell(cell)
                var center = cell.center

                My.cellSnapshot!.center = center

                My.cellSnapshot!.alpha = 0.0

                table.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:
            var center = My.cellSnapshot!.center
            center.y = locationInView.y
            My.cellSnapshot!.center = center
            if ((indexPath != nil) && (indexPath != Path.initialIndexPath)) {
                swap(&cellContent[indexPath!.row], &cellContent[Path.initialIndexPath!.row])
                table.moveRowAtIndexPath(Path.initialIndexPath!, toIndexPath: indexPath!)
                Path.initialIndexPath = indexPath
            }

        default:
            let cell = table.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


                    }
            })

        }

}

问题是在长按电池时会崩溃。

错误代码:longPressGestureRecognized:]:无法识别的选择器发送到实例0x79a3b110'

其他一切工作正常,如删除单元格内容等。

提前致谢!

1 个答案:

答案 0 :(得分:0)

请再次检查你的功能“func longPressGestureRecognized(gestureRecognizer:UILongPressGestureRecognizer)” 我测试了你的代码,一切正常。我可能会怀疑你把它放在其他功能中。

相关问题