【问题标题】:Drag & Drop from TableView to Another View in Swift在 Swift 中从 TableView 拖放到另一个视图
【发布时间】:2016-11-14 09:42:23
【问题描述】:

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

这是我的 UI 架构的小草图

这就是我正在做的事情

  1. 长按表格视图中的一行

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

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

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

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

谁能提供一些关于如何通过一些示例代码解决问题的见解...

提前谢谢...

【问题讨论】:

  • 如果您对代码或部分代码有其他疑问,请给我写评论,我会在几个小时内给您回信......
  • @Jobins John 你能给我这段代码的客观 c 版本吗?
  • 你能在这里发布你的工作代码吗。我也有同样的要求。

标签: ios swift uitableview drag-and-drop swift2


【解决方案1】:

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

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
    //...
}

【讨论】:

  • 非常感谢@Neo...我能够通过对您的答案进行一些修改来解决问题。但是您的回答确实帮助我解决了问题。
  • 没问题,伙计,玩得开心...(别忘了我的赞成票...):-)
  • 在 didLongPressCell 函数中是否有任何选项可以访问长按单元格的元素。假设我在长按单元格中有一个图像视图,并且我想在该函数中访问它。有什么办法吗?
  • 有没有不给标签的选项?
  • 嗯,在这个宪法中,我认为没有不提供标签的选项......因为你只是在移动一个 UIView......也许如果你将 contentView 子类化,给它文本字段和图像视图因为属性可能是一种方式...但这将是更多的代码/工作...我认为这不是一个好主意...您是否有理由不想使用标签?
【解决方案2】:

义人的道路被复杂性所困扰。 如果你够男子气概

  1. 放弃 ios 10 及更早版本支持将部署目标提升到 ios 11
  2. 采用 UITableViewDragDelegate 协议 2.1 [可选]为表格启用dragInteractionEnabled = true 让它在 iphone 上工作(默认情况下,拖动只在 ipad 上工作)
  3. 学习https://developer.apple.com/documentation/uikit/drag_and_drop/making_a_view_into_a_drop_destination 检查如何使绿色区域成为放置目的地
  4. 在 wwdc 2017 中观看大约 4 个拖放相关视频或浏览 他们的 ascii 版本

作为奖励,您可以在应用程序之间进行拖放操作,而不是 就在您的应用程序中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-11
    • 1970-01-01
    • 2013-08-08
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 2011-01-31
    相关资源
    最近更新 更多