ios - 从 TableView 拖放到 Swift 中的另一个 View

标签 ios swift uitableview drag-and-drop swift2

我正在尝试将一个项目从一个 UITableView 复制到另一个 View,过去 2 天我一直在努力解决这个问题,但我仍然无法弄清楚如何完成此操作。

这是我的 UI 架构的草图

enter image description here

这是我在做的事情

  1. 长按表格 View 中的一行

  2. 长按时创建单元格中图像的快照

  3. 将快照拖到表格 View 外的View(绿色区域)

  4. 发布时检查快照是否被放置在绿色 View 区。

我能够做到直到创建快照为止,当我尝试拖动快照时,我无法获得拖动快照的点。释放拖动时,我需要检查拖动的最后一点是否在 DROP AREA(绿色)内。

任何人都可以提供一些关于如何使用示例代码解决问题的见解......

提前致谢...

最佳答案

目前我没有时间测试代码,但它应该足够有意义......你可以这样做:

class ViewController: UIViewController, UITableViewDataSource {

    private var dragView: UIView?
    @IBOutlet weak var dropZone: UIView!

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)

        let lpGestureRecognizer: UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(didLongPressCell))
        cell.contentView.addGestureRecognizer(lpGestureRecognizer)

        return cell
    }

    func didLongPressCell (recognizer: UILongPressGestureRecognizer) {
        switch recognizer.state {
        case .Began:
            if let cellView: UIView = recognizer.view {
                cellView.frame.origin = CGPointZero
                dragView = cellView
                view.addSubview(dragView!)
            }
        case .Changed:
            dragView?.center = recognizer.locationInView(view)
        case .Ended:
            if (dragView == nil) {return}

            if (CGRectIntersectsRect(dragView!.frame, dropZone.frame)) {
                if let cellView: UIView = (dragView?.subviews[0])! as UIView {
                    cellView.frame.origin = CGPointZero
                    dropZone.addSubview(cellView)
                }

                dragView?.removeFromSuperview()
                dragView = nil

                //Delete row from UITableView if needed...
            } else {
                //DragView was not dropped in dropszone... Rewind animation...
            }
        default:
            print("Any other action?")
        }
    }

}

评论更新:

当然,一种可能是标记字段...像这样:

private let imageViewTag: Int = 997
private let textLabelTag: Int = 998
private let detailTtextLabelTag: Int = 999

//...

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    //...
    cell.imageView?.tag = imageViewTag
    cell.textLabel?.tag = textLabelTag
    cell.detailTextLabel?.tag = detailTtextLabelTag
    //...

}

func didLongPressCell (recognizer: UILongPressGestureRecognizer) {
    //...
    case .Ended:
    let cellImageView: UIImageView? = recognizer.view?.viewWithTag(imageViewTag) as? UIImageView
    let cellTextLabel: UITextField? = recognizer.view?.viewWithTag(textLabelTag) as? UITextField
    let cellDetailTextLabel: UITextField? = recognizer.view?.viewWithTag(detailTtextLabelTag) as? UITextField
    //...
}

关于ios - 从 TableView 拖放到 Swift 中的另一个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38323948/

相关文章:

ios - UIImageView/TableFooterView 上的两个 init 方法

objective-c - 注释在 MapKit 、 iOS 上不显示标题/副标题

ios - 将静态 UILabel 放在滚动的 UITableView 之上

arrays - 如何按属性类型自动排序 Swift 中的数组?

swift - 'OpenExternalURLOptionsKey' is not a member type of 'UIApplication' Auth0问题

ios - 在 View Controller Swift 中的 UITableView 中加载多个 UICollectionView

ios - 如何打开框架中包含的 View Controller 作为 iOS 的静态库?

iOS 部署 : How to install a certificate locally to distribute app under different dev team?

ios - 关闭作为 UITableViewCell 子类的属性以更新值 : Is it a bad idea?

ios - 当我按下按钮时,我想要添加文本字段的内容 - IOS TableView