简体   繁体   中英

editButtonItem Does Not Work

I have a table view inside a UIViewController with editing enabled for deleting rows. Swiping from the right lets me delete rows and I have the edit button on the navigation bar but it doesn't actually do anything except switch from saying Edit to Done.

Here is how I'm creating my table view.

override func viewWillAppear(animated: Bool) {
    for view in self.view.subviews {
        view.removeFromSuperview()
    }

    if sharedCart.shoppingCart.isEmpty {
        self.navigationItem.rightBarButtonItem = nil

        isEmptyLabel = UILabel(frame: CGRectMake(self.view.frame.width / 2, self.view.frame.height / 2, self.view.frame.width, self.view.frame.height))
        isEmptyLabel.center = self.view.center
        isEmptyLabel.text = "Your cart is empty."
        isEmptyLabel.textAlignment = .Center
        isEmptyLabel.textColor = UIColor.whiteColor()
        isEmptyLabel.font = UIFont(name: "Helvetica-Light", size: 20.0)

        self.view.addSubview(isEmptyLabel)
    } else {
        isEmptyLabel.removeFromSuperview()
        total = 0

        let editItem = self.editButtonItem()
        self.navigationItem.rightBarButtonItem = editItem

        tableView = UITableView(frame: self.view.frame, style: .Grouped)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = UIColor.clearColor()
        tableView.contentInset = UIEdgeInsetsMake(44, 0, 0, 0)

        tableView.registerNib(UINib(nibName: "CartCell", bundle: nil), forCellReuseIdentifier: "passCartCell")
        tableView.registerNib(UINib(nibName: "CartFooterView", bundle: nil), forHeaderFooterViewReuseIdentifier: "cartFooter")

        self.view.addSubview(tableView)
}

And I use these methods for editing.

func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    return true
}

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == .Delete {
        sharedCart.shoppingCart.removeAtIndex(indexPath.row)

        self.viewWillAppear(true)
    }
}

Cedric Michael's answer almost works, but it disables editButtonItem 's automatic, animated toggling between the Edit and Done title & associated state. The better fix is this:

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

Also, if you want to hide the Edit button for an empty shopping cart, it would be better form to set navigationItem.rightBarButtonItem = editButtonItem in viewDidLoad() and simply set navigationItem.rightBarButtonItem.isHidden to true or false in viewWillAppear() according to the shopping cart.

For Swift 3 , implement this in your UIViewController class:

override func setEditing(_ editing: Bool, animated: Bool) {
    if(editing && !tableView.isEditing){
        tableView.setEditing(true, animated: true)
    }else{
        tableView.setEditing(false, animated: true)
    }
}

(This is not necessary for a UITableViewController class).

You should also set your editButtonItem before :

override func viewDidLoad() {
    super.viewDidLoad()
    navigationItem.rightBarButtonItem = editButtonItem
}

Then it works!

In order for your table to toggle between edit and normal mode on button tap you need to implement

func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
        if self.tableView.editing {
            return UITableViewCellEditingStyle.Delete
        }
        else{
            return UITableViewCellEditingStyle.None
        }
    }

And on button tap or IBAction of button :)

@IBAction func editButtonTapped(sender: AnyObject) {
    var button =  sender as! UIButton
    if button.titleLabel!.text! == "Edit" {
        self.tableView.editing = true
        button.setTitle("Done", forState: UIControlState.Normal)
    }
    else{
        self.tableView.editing = false
        button.setTitle("Edit", forState: UIControlState.Normal)
    }
}

EDIT Just realised you have bar button item on navigation bar rather then plain UIButton :)

So you can modify the IBAction as below :) Make sure the you select System Item for UIBarButton item as Custom and set the title as Edit

@IBAction func editButtonTapped(sender: AnyObject) {
    let button =  sender as! UIBarButtonItem
    if button.title! == "Edit" {
        self.tableView.editing = true
        button.title = "Done"
    }
    else{
        self.tableView.editing = false
        button.title = "Edit"
    }
}

This should do the job :) Happy coding :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM