使用UITableView

时间:2016-01-12 16:37:49

标签: ios swift tableview long-press uilongpressgesturerecogni

我正在使用ViewController中的tableView开发应用程序。在此表格中我有部分,我希望我的用户能够使用 UILongPressGestureRecognizer 长按,拖放部分之间的单元格。

使用以下代码,我的手势识别器可以正常工作,但是当我尝试拖动单元格并将其放在tableView中的任何位置时,我的应用程序崩溃了。崩溃发生在两个不同的行中。当单元格被长按并在一个部分内拖动时,它会在此行崩溃:tableView.moveRowAtIndexPath(Path.initialIndexPath!, toIndexPath: indexPath!) 如果我试图拖出某个部分,它会在此行崩溃:swap(&Categories.categories[indexPath!.row], &Categories.categories[Path.initialIndexPath!.row])

(包含每个部分和行的数据的数组位于一个单独的类中,名为Categories。带有部分的数组称为“sections”,带有行的数组称为“categories”。)

我很感激任何帮助,找出它失败的原因以及如何解决它。

func setupLongPress() {
    let longpress = UILongPressGestureRecognizer(target: self, action:   "longPressGestureRecognized:")
    tableView.addGestureRecognizer(longpress)
}

func longPressGestureRecognized(gestureRecognizer: UIGestureRecognizer) {
    let longPress = gestureRecognizer as! UILongPressGestureRecognizer
    let state = longPress.state
    let locationInView = longPress.locationInView(tableView)
    let indexPath = tableView.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 = tableView.cellForRowAtIndexPath(indexPath!) as UITableViewCell!
            My.cellSnapshot  = snapshopOfCell(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:
        var center = My.cellSnapshot!.center
        center.y = locationInView.y
        My.cellSnapshot!.center = center
        if ((indexPath != nil) && (indexPath != Path.initialIndexPath)) {
            swap(&Categories.categories[indexPath!.row], &Categories.categories[Path.initialIndexPath!.row])
            tableView.moveRowAtIndexPath(Path.initialIndexPath!, toIndexPath: indexPath!)
            Path.initialIndexPath = indexPath
        }

    default:
        let cell = tableView.cellForRowAtIndexPath(Path.initialIndexPath!) as UITableViewCell!
        cell.hidden = false
        cell.alpha = 0.0
        UIView.animateWithDuration(0.5, 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
            }
        })
    }
}

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 = 1.0
            cellSnapshot.layer.shadowOpacity = 0.2
    return cellSnapshot
}

谢谢!

0 个答案:

没有答案