Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
557 views
in Technique[技术] by (71.8m points)

iphone - Reordering controls on UITableView while not in editing mode?

I have a UITableView and I want to be able to reorder the rows when I'm not in edit mode, meaning I don't want to see the delete icons until I press Edit. I want to be able to see and use the reordering controls all the time. Is that possible?

Do I have to keep the UITableView in editing mode all the time and enable/disable the delete icon manually? How to disable the swipe to delete in that case?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I found a solution. UITableViewCell refuses to draw the reorder control unless it's in editing mode. Fortunately, UITableViewCell and UITableView track editing separately, and crucially, UITableView actually handles reordering regardless of its own editing mode. We just need to trick the cells into drawing the reorder controls and we're home free.

Subclass UITableViewCell like this:

class ReorderTableViewCell: UITableViewCell {

    override var showsReorderControl: Bool {
        get {
            return true // short-circuit to on
        }
        set { }
    }

    override func setEditing(editing: Bool, animated: Bool) {
        if editing == false {
            return // ignore any attempts to turn it off
        }

        super.setEditing(editing, animated: animated)
    }

}

Now simply set editing = true on cells for which you want to enable reordering. You can make it conditional on -tableView:canMoveRowAtIndexPath:.

In your table view data source:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)

    // Configure the cell...
   :

    cell.editing = self.tableView(tableView, canMoveRowAtIndexPath: indexPath) // conditionally enable reordering

    return cell
}

The only drawback is that this is incompatible with the table view allowsMultipleSelectionDuringEditing option; the edit control is incorrectly always shown. A workaround is to enable multiple selection only during actual table view editing.

In your table view controller:

override func setEditing(editing: Bool, animated: Bool) {
    super.setEditing(editing, animated: animated)

    self.tableView.allowsMultipleSelectionDuringEditing = editing // enable only during actual editing to avoid cosmetic problem
}

Also, in -viewDidLoad or your storyboard, set the initial value of allowsMultipleSelectionDuringEditing to false.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...