【问题标题】:Trying to create drag and drop to tableview. SWIFT尝试创建拖放到 tableview。迅速
【发布时间】:2015-03-08 17:04:48
【问题描述】:

我一直在尝试使用本指南创建拖放操作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 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'

其他一切都很好,比如删除单元格内容等。

提前致谢!

【问题讨论】:

    标签: swift drag-and-drop tableview cell


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多