从TableView拖放到Swift中的另一个视图

我正在尝试将一个项目从一个UITableView复制到另一个视图,过去两天我一直在敲打这个问题,但我仍然无法弄清楚如何实现这一目标。

这是我的UI架构的一个小草图

在此处输入图像描述

这就是我在做的事情

  1. 长按在tableview中的一行

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

  3. 将快照拖动到表视图外部的视图(绿色区域)

  4. 发布时检查快照是否在绿色视图区域中被删除。

我能够做到快照创建点,当我尝试拖动快照时,我无法获得拖动快照的点。 当拖动被释放时,我需要检查最后一个拖动点是否在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 //... }