Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to disable commitEditingStyle programmatically?

In my tableview, I only want certain cells to be able to drag to the left for some options based on a condition. The other cells should behave as if commitEditingStyle is disabled. Is this possible?

With the code below, I can add the actions when the conditions are met but the other cells still get the default "delete" action. How do I get rid of that delete action?

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
}

override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {

    let object = items[indexPath.row]
    if object.name == "name" {

        // someAction
        var addAction = UITableViewRowAction(style: .Default, title: "+") { (action: UITableViewRowAction!, indexPath: NSIndexPath!) -> Void in
        }
        return [addAction]
    }
    return nil
}

With the code below I managed to enable and disable the actions. But only with the Delete button.

override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {

    let object = items[indexPath.row]
    if object.name == "joyce" {
        return UITableViewCellEditingStyle.Delete
    } else {
        return UITableViewCellEditingStyle.None
    }
}
like image 828
Hendrik Smoels Avatar asked Mar 17 '15 21:03

Hendrik Smoels


Video Answer


2 Answers

You'll want a way to determine editable state based on your data model. For example:

class Message
{
    var subject : String
    var title : String
    var isEditable : Bool

    init(subject: String, title: String)
    {
        self.subject = subject
        self.title = title
        self.isEditable = true
    }
}

With this, you can easily handle the tableView:canEditRowAtIndexPath: delegate method. Your view controller should look something like this:

class ViewController : UIViewController, UITableViewDataSource, UITableViewDelegate
{
    var tableView : UITableView!
    var messages : [Message]

    // MARK: - UITableView Delegate

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return self.messages.count
    }

    func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool
    {
        let message = self.messages[indexPath.row]
        return message.isEditable
    }
}

In some more complex examples it may be a computed property but the overall concept is the same.

like image 101
Erik Avatar answered Nov 02 '22 04:11

Erik


It sounds like you're looking for optional func tableView(_ tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool.

From Apple documentation:

The method permits the data source to exclude individual rows from being treated as editable. Editable rows display the insertion or deletion control in their cells. If this method is not implemented, all rows are assumed to be editable.

like image 37
Phillip Mills Avatar answered Nov 02 '22 05:11

Phillip Mills