在没有重新排序控制的情况下重新排序 UITableView [英] Reordering UITableView without reorder control

查看:21
本文介绍了在没有重新排序控制的情况下重新排序 UITableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用户能够通过这种方式重新排序 UITableView:他触摸一个单元格一段预定的时间(例如 1 秒),然后他可以将其拖放到其他单元格上.

I need the user to be able to reorder a UITableView by this way: he touches a cell for a predetermined period (e.g. 1 second), then he can drag and drop it over the other cells.

我知道如何使用手势识别器实现长触摸"检测,但是在不使用重新排序控件的情况下实现拖放功能的最佳方法是什么(用户应该从单元格中的任何位置拖动单元格,不仅来自重新排序控件)?

I know how to implement the 'long touch' detection using a gesture recognizer, but what is the best way to implement the drag and drop ability without using a reorder control (the user should drag the cell from anywhere in the cell, not only from the reorder control)?

推荐答案

这是一个老问题,但这里有一个经过测试并在 iOS 811 上工作的解决方案.

This is an old question, but here's a solution that's tested and working with iOS 8 through 11.

在你的 UITableViewCell 子类中试试这个:

In your UITableViewCell subclass try this:

class MyTableViewCell: UITableViewCell {
    weak var reorderControl: UIView?

    override func layoutSubviews() {
        super.layoutSubviews()

        // Make the cell's `contentView` as big as the entire cell.
        contentView.frame = bounds

        // Make the reorder control as big as the entire cell 
        // so you can drag from everywhere inside the cell.
        reorderControl?.frame = bounds
    }

    override func setEditing(_ editing: Bool, animated: Bool) {
        super.setEditing(editing, animated: false)
        if !editing || reorderControl != nil {
            return
        }

        // Find the reorder control in the cell's subviews.
        for view in subviews {
            let className = String(describing: type(of:view))
            if className == "UITableViewCellReorderControl" {

                // Remove its subviews so that they don't mess up
                // your own content's appearance.
                for subview in view.subviews {
                    subview.removeFromSuperview()
                }

                // Keep a weak reference to it for `layoutSubviews()`.
                reorderControl = view

                break
            }
        }
    }
}

这接近 Senseful 的第一个建议,但他引用的文章似乎不再有效.

It's close to Senseful's first suggestion but the article he references no longer seems to work.

您所做的是使重新排序控件和单元格的内容视图在编辑时与整个单元格一样大.这样您就可以从单元格内的任何位置拖动,并且您的内容会占据整个空间,就好像单元格根本没有被编辑一样.

What you do, is make the reorder control and the cell's content view as big as the whole cell when it's being edited. That way you can drag from anywhere within the cell and your content takes up the entire space, as if the cell was not being edited at all.

最重要的缺点是您正在更改系统的单元格视图结构并引用私有类 (UITableViewCellReorderControl).它似乎适用于所有最新的 iOS 版本,但您必须确保每次新操作系统出现时它仍然有效.

The most important downside to this, is that you are altering the system's cell view-structure and referencing a private class (UITableViewCellReorderControl). It seems to be working properly for all latest iOS versions, but you have to make sure it's still valid every time a new OS comes out.

这篇关于在没有重新排序控制的情况下重新排序 UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆