我已经以编程方式创建了UITableView
。我现在正尝试使用编辑模式添加删除行的功能。
我添加了默认的执行方式:
self.navigationItem.leftBarButtonItem = self.editButtonItem;
点击按钮后,它进入编辑模式,按钮变为“完成”按钮。
但是,当我“滑动删除”显示删除按钮的行时,现有的编辑按钮不会更改为“已完成”。
我是否需要做额外的事情,因为我以编程方式创建了表格视图?
以下是我的tableview的代码:
- (void)viewDidLoad
{
self.voucherTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 36, 320, self.view.frame.size.height - 36) style:UITableViewStylePlain];
self.voucherTableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
self.voucherTableView.delegate = self;
self.voucherTableView.dataSource = self;
[self.view addSubview:self.voucherTableView];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
// Default table view data source methods go here
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[self.voucherTableView setEditing:editing animated:animated];
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// Commit the delete
}
}
答案 0 :(得分:3)
以下是我的答案:按钮不会更改为“完成”,因为要删除的幻灯片实际上不必在“编辑”模式下完成,并且它不会让您进入编辑模式。
如果您希望执行删除操作,可以强制它进入编辑模式,不知道为什么要这样做?
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[tableView setEditing:YES animated:YES];
// Commit the delete
}
}
答案 1 :(得分:1)
新功能,从iOS 8开始的当前版本
只要您使用self.editingItem
,它现在应该自行更新。事实上,Apple的-tableView:commitEditingStyle:forRowAtIndexPath:
文档现在具体说:
您不应在此方法的实现中调用
-setEditing:animated:
。如果由于某种原因你必须在延迟后使用-performSelector:withObject:afterDelay:
方法调用它。
这是对其他一些答案中提到的先前功能的更改。
需要注意的一点是,从iOS 8.1.2开始,如果您未能在-deleteRowsAtIndexPaths:withRowAnimation:
中调用-tableView:commitEditingStyle:forRowAtIndexPath:
(例如,在更新数据模型后调用[self.tableView reloadData]
) editingItem将停留在编辑状态。
答案 2 :(得分:0)
@Scrooby:编辑按钮标题和状态不会更改为“完成”
这是因为“编辑”按钮单击会触发tableview的编辑模式,但是tableview的编辑模式不会触发事件将按钮标题更改为“完成”。
假设编辑按钮单击(事件A)&编辑模式下的TableView(事件B)
事件A表示事件B,但事件B并不表示事件A.
希望你现在明白。这是一个简单的逻辑。
希望这会对你有所帮助。
答案 3 :(得分:0)
尝试将[super setEditing:editing animated:animated];
放在方法的末尾。它应该工作。