UITableview edit/done button

喜你入骨 提交于 2020-01-10 08:21:08

问题


I have a tableview and navigation bar on the top.

I have a Edit button on the left of my navigation bar with the following line of code.

self.navigationItem.leftBarButtonItem = self.editButtonItem;

When i click on the edit button, it changes to done button. All is fine so far.

Where do i add code, if i want to do a small operation when the Done button is clicked.?


回答1:


The button stops committing the changes to your controller class once you override it's default action with self.editButtonItem.action = @selector(editClicked:);

What you should do is override UIViewController's setEditing method in your own controller class:

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];

    if(editing == YES) 
    {
        // Your code for entering edit mode goes here
    } else {
        // Your code for exiting edit mode goes here
    }
}

You also need to set your UIBarButtonItem to "Edit" in storyboard or if you prefer doing it in code use the following:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

editButtonItem is a helper property already set by the system for your comfort.




回答2:


Here is a Swift version I used:

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

    if editing {

    } else {

    }
}



回答3:


thats what i did on Swift 4:

this is create bar button in viewDidLoad():

// init barbutton and set default to true
    self.navigationItem.rightBarButtonItem = self.editButtonItem
    super.isEditing = true

add override setEditing() below the viewDidLoad():

    override func setEditing (_ editing:Bool, animated:Bool)
{
    super.setEditing(editing,animated:animated)
    if(self.isEditing)
    {
        self.editButtonItem.title = "Edit"
    }else
    {
        self.editButtonItem.title = "Done"
    }
}


来源:https://stackoverflow.com/questions/5436520/uitableview-edit-done-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!