我有一个带有tableView的UIViewController类。在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
答案 0 :(得分:22)
为什么不直接使用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.
}
}
答案 1 :(得分:2)
这是因为您在切换模式时只设置了按钮的样式(而不是文本)。您需要这样做(或等效):
-(void)toggleEditMode{
self.navigationItem.rightBarButtonItem.title = self.tableView.editing ? @"Done" : @"Edit";
答案 2 :(得分:0)