如何交换两个不同表视图的两个单元格?

时间:2017-12-24 15:26:10

标签: ios swift

我遇到了这个问题: 我正在使用此代码交换同一个表视图的两个单元格。我想问你应该如何编辑这个代码来交换同一控制器中两个不同表视图的两个单元格?我认为编辑的内容不多,但我不能自己做...谢谢大家。

@objc func onLongPressGestureDone(sender: UILongPressGestureRecognizer) {

    let locationInView = sender.location(in: tableViewDone)
    let indexPath = tableViewDone.indexPathForRow(at: locationInView)

    if sender.state == .began {
        if indexPath != nil {
            initialIndexPathDone = indexPath
            let cell = tableViewDone.cellForRow(at: indexPath!)
            cellSnapshotDone = snapshotOfCellDone(inputView: cell!)
            var center = cell?.center
            cellSnapshotDone?.center = center!
            cellSnapshotDone?.alpha = 0.0
            tableViewDone.addSubview(cellSnapshotDone!)

            UIView.animate(withDuration: 0.25, animations: { () -> Void in
                center?.y = locationInView.y
                self.cellSnapshotDone?.center = center!
                self.cellSnapshotDone?.transform = (self.cellSnapshotDone?.transform.scaledBy(x: 1.05, y: 1.05))!
                self.cellSnapshotDone?.alpha = 0.99
                cell?.alpha = 0.0
            }, completion: { (finished) -> Void in
                if finished {
                    cell?.isHidden = true
                }
            })
        }
    } else if sender.state == .changed {
        var center = cellSnapshotDone?.center
        center?.y = locationInView.y
        cellSnapshotDone?.center = center!

        if ((indexPath != nil) && (indexPath != initialIndexPathDone)) {
            progetto.done.swapAt(indexPath!.row, initialIndexPathDone!.row)
            tableViewDone.moveRow(at: initialIndexPathDone!, to: indexPath!)
            initialIndexPathDone = indexPath
        }
    } else if sender.state == .ended {
        let cell = tableViewDone.cellForRow(at: initialIndexPathDone!)
        cell?.isHidden = false
        cell?.alpha = 0.0
        UIView.animate(withDuration: 0.25, animations: { () -> Void in
            self.cellSnapshotDone?.center = (cell?.center)!
            self.cellSnapshotDone?.transform = CGAffineTransform.identity
            self.cellSnapshotDone?.alpha = 0.0
            cell?.alpha = 1.0
        }, completion: { (finished) -> Void in
            if finished {
                self.initialIndexPathDone = nil
                self.cellSnapshotDone?.removeFromSuperview()
                self.cellSnapshotDone = nil
            }
        })
    }
}

func snapshotOfCellDone(inputView: UIView) -> UIView {
    UIGraphicsBeginImageContextWithOptions(inputView.bounds.size, false, 0.0)
    inputView.layer.render(in: UIGraphicsGetCurrentContext()!)
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    let cellSnapshotDone = UIImageView(image: image)
    cellSnapshotDone.layer.masksToBounds = false
    cellSnapshotDone.layer.cornerRadius = 0.0
    cellSnapshotDone.layer.shadowOffset = CGSize(width: -5.0, height: 0.0)
    cellSnapshotDone.layer.shadowRadius = 5.0
    cellSnapshotDone.layer.shadowOpacity = 0.4
    return cellSnapshotDone
}

1 个答案:

答案 0 :(得分:0)

使用moveRow(at:to:)功能可以更轻松地完成此操作:

tableView.beginUpdates()
tableView.moveRow(at: firstIndexPath, to: secondIndexPath)
tableView.moveRow(at: secondIndexPath, to: firstIndexPath)
tableView.endUpdates()