Reordering UITableView without reorder control

前端 未结 5 1152
挽巷
挽巷 2020-12-10 03:17

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 ce

5条回答
  •  长情又很酷
    2020-12-10 03:54

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

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

    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.

    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.

提交回复
热议问题