UITableView 编辑/完成事件

发布于 2024-10-07 07:37:26 字数 247 浏览 6 评论 0原文

是否有通知或委托方法可以用来检测表视图何时进入编辑状态?

我想要做的是检测表格正在编辑,然后显示一个额外的行,上面写着“添加新项目”或类似的内容。

我尝试在加载视图控制器时在数组末尾添加“添加新项目”行,然后根据 [tableView isEditing] 是否为 true,返回 [array count] (当我编辑时)或[数组计数] - 1(当我不编辑时)。

有什么想法吗? Apple 编辑表中的项目并允许删除的方式是什么?

Is there a notification or delegate method that I can use to detect when the table view goes into the editing state?

What I want to do is detect that the table is editing and then display an extra row that says "Add new item" or something like that.

I tried adding the "Add New Item" row at the end of the array when the View Controller is loaded and then depending on whether [tableView isEditing] is true or not, either return [array count] (for when I am editing) or [array count] - 1 (for when I am not editing).

Any ideas? What is the way Apple edits items in the table and allows for deletion?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吹泡泡o 2024-10-14 07:37:26

我找到了。重写这个方法:

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

    // do something
}

I found it. Override this method:

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

    // do something
}
小嗷兮 2024-10-14 07:37:26

您可以做的是将 IBAction 作为选择器添加到您的 editButton 中。当点击 editButton 时,将调用该方法。示例:

-(void)viewDidLoad
{
// ...
[self.editButtonItem setAction:@selector(editAction:)];
[self.navigationItem setRightBarButtonItem: self.editButtonItem];

// .. your code

}

-(IBAction)editAction:(id)sender
{
    UIBarButtonItem * button = ((UIBarButtonItem*)sender);

    if (!self.tableView.editing)
    {
        [self.tableView setEditing:YES animated:YES];
        [button setTitle:@"Done"];
        // do your stuff...
    }
    else
    {
        [button setTitle:@"Edit"];
        [self.tableView setEditing:NO animated:YES];
        // do your stuff...
    }
}

如果您有自己的 UIButton 并且没有使用标准 self.editButtonItem,则使用
[yourButton addTarget:self action:@selector(editAction:) forControlEvents:UIControlEventTouchUpInside];
并在 editAction: 方法中将其作为 UIButton* 处理

What you can do is to add an IBAction as a selector to your editButton. When editButton is tapped that method will be called. Example:

-(void)viewDidLoad
{
// ...
[self.editButtonItem setAction:@selector(editAction:)];
[self.navigationItem setRightBarButtonItem: self.editButtonItem];

// .. your code

}

-(IBAction)editAction:(id)sender
{
    UIBarButtonItem * button = ((UIBarButtonItem*)sender);

    if (!self.tableView.editing)
    {
        [self.tableView setEditing:YES animated:YES];
        [button setTitle:@"Done"];
        // do your stuff...
    }
    else
    {
        [button setTitle:@"Edit"];
        [self.tableView setEditing:NO animated:YES];
        // do your stuff...
    }
}

If you have your own UIButton and didn't use standard self.editButtonItem then use
[yourButton addTarget:self action:@selector(editAction:) forControlEvents:UIControlEventTouchUpInside];
And handle it as a UIButton* in editAction: method

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文