UITableView 处于编辑模式 - “编辑”按钮不会改变状态

发布于 2024-10-23 11:36:48 字数 1154 浏览 4 评论 0原文

我有一个 UIViewController 类,带有一个 tableView。在 viewDidLoad 中:

UIBarButtonItem *editIcon = [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem: UIBarButtonSystemItemEdit
                    target:self
                    action:@selector(toggleEditMode)] autorelease];

在方法 'toggleEditMode' 中:

-(void)toggleEditMode{
if(self.theTable.editing) {
    [theTable setEditing:NO animated:YES];
    [self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStylePlain];
}
else if ([callsArray count]!=0){
    [theTable setEditing:YES animated:YES];
    [self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone];
}

}

问题是“编辑”按钮不会更改为“完成”。缺少什么?我已经声明了所有方法:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

谢谢,

RL

I have a UIViewController class, with a tableView. In viewDidLoad:

UIBarButtonItem *editIcon = [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem: UIBarButtonSystemItemEdit
                    target:self
                    action:@selector(toggleEditMode)] autorelease];

In te method 'toggleEditMode':

-(void)toggleEditMode{
if(self.theTable.editing) {
    [theTable setEditing:NO animated:YES];
    [self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStylePlain];
}
else if ([callsArray count]!=0){
    [theTable setEditing:YES animated:YES];
    [self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone];
}

}

The problem is that the Edit button does not change do 'DONE'. What's missing? I have all the methods declared:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

Thanks,

RL

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

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

发布评论

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

评论(3

十秒萌定你 2024-10-30 11:36:48

为什么不直接使用 UIViewController 的 -editButtonItem ?并重写 -setEditing:animated: 方法。

// Assign the system's edit button, 
// it will change style when different edit status.
self.navigationItem.rightBarButtonItem = self.editButtonItem;

// The editButtonItem will invoke this method.
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];

    if (editing) {
         // Execute tasks for editing status
    } else {
         // Execute tasks for non-editing status.
    }
}

Why not use -editButtonItem of UIViewController directly ? And override -setEditing:animated: method.

// Assign the system's edit button, 
// it will change style when different edit status.
self.navigationItem.rightBarButtonItem = self.editButtonItem;

// The editButtonItem will invoke this method.
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];

    if (editing) {
         // Execute tasks for editing status
    } else {
         // Execute tasks for non-editing status.
    }
}
萌吟 2024-10-30 11:36:48

这是因为当您切换模式时,您仅设置按钮的样式(而不是其文本)。您需要执行此操作(或等效操作):

-(void)toggleEditMode{
    self.navigationItem.rightBarButtonItem.title = self.tableView.editing ? @"Done" : @"Edit";

It's because you're only setting the style of the button (not its text) when you switch modes. You need to do this (or equivalent):

-(void)toggleEditMode{
    self.navigationItem.rightBarButtonItem.title = self.tableView.editing ? @"Done" : @"Edit";
岁月静好 2024-10-30 11:36:48

您确定 callsArray 不为空吗?

toggleEditMode 内放置一个断点,看看会发生什么。

编辑

好的,在了解你的问题后,看看这个线程

You sure callsArray is not empty ?

Place a breakpoint inside toggleEditMode and see what happens there.

Edit

Ok, after understanding your question, have a look at this thread

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