简体   繁体   English

UITableview编辑/完成按钮

[英]UITableview edit/done button

I have a tableview and navigation bar on the top. 我的顶部有一个tableview和导航栏。

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.? 如果我想在单击完成按钮时执行小操作,我在哪里添加代码?

The button stops committing the changes to your controller class once you override it's default action with self.editButtonItem.action = @selector(editClicked:); 一旦用self.editButtonItem.action = @selector(editClicked:);覆盖它的默认操作,该按钮就会停止将更改提交到控制器类self.editButtonItem.action = @selector(editClicked:);

What you should do is override UIViewController's setEditing method in your own controller class: 你应该做的是在你自己的控制器类中覆盖UIViewController的setEditing方法:

- (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: 您还需要在故事板中将UIBarButtonItem设置为“Edit”,或者如果您更喜欢在代码中执行此操作,请使用以下命令:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

editButtonItem is a helper property already set by the system for your comfort. editButtonItem是系统已设置的助手属性,以方便您使用。

Here is a Swift version I used: 这是我使用的Swift版本:

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

    if editing {

    } else {

    }
}

thats what i did on Swift 4: 这就是我在Swift 4上所做的:

this is create bar button in viewDidLoad(): 这是viewDidLoad()中的创建栏按钮:

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

add override setEditing() below the viewDidLoad(): 在viewDidLoad()下面添加override setEditing():

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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