我试图将一个项目从一个UITableView复制到另一个视图,过去两天我一直在敲打这个问题,但我仍然无法弄清楚如何实现这一目标。
这是我的UI架构的一个小草图
这就是我正在做的事情
长按表格视图中的一行
长按
将快照拖动到表格视图外的视图(绿色区域)
发布时检查快照是否已在绿色视图区域中删除。
我能够做到快照创建点,当我尝试拖动快照时,我无法获得拖动快照的点。当拖动被释放时,我需要检查最后一个拖动点是否在DROP AREA(绿色)内。
任何人都可以通过一些示例代码提供一些有关如何解决问题的见解......
提前致谢...
答案 0 :(得分:5)
目前我没有时间测试代码,但它应该足够有意义....你可以这样做:
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
//...
}
答案 1 :(得分:1)
作为奖励,您可以在应用程序之间进行拖放操作,而不是 就在您的应用内。