这可能是一个非常愚蠢的问题,但我对Xcode,Objective-C和iOS开发知之甚少。
我所拥有的是一个UITableViewController,有许多单元格。我已经得到'添加'和删除(滑动删除)功能,但我无法弄清楚如何将'编辑'按钮连接到一个将触发该TableView的编辑模式的功能,所以我可以重新排列元素的顺序。
我没有评论这些功能:
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
XYZToDoItem *itemToMove = [self.toDoItems objectAtIndex:fromIndexPath.row];
[self.toDoItems removeObjectAtIndex:fromIndexPath.row];
[self.toDoItems insertObject:itemToMove atIndex:toIndexPath.row];
[self.tableView reloadData];
}
正如您所看到的,我在文档后面还对moveRowAtIndexPath方法做了一些补充。
此外还有这些功能;
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[self.toDoItems removeObjectAtIndex:indexPath.row];
[self.tableView reloadData];
}
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
如前所述“滑动删除”功能有效,我不知道如何,但我只是简单地取消注释这两个方法并编辑了一个以适合我的代码。
更具体地说,我问的是如何链接我在导航控制器中的“编辑”按钮来触发TableView的编辑模式,从而显示TableViews中已知的三行(?)编辑模式(让我将元素拖动到我想要的顺序)。
提前感谢您的帮助! :)
答案 0 :(得分:0)
如果这是一个UITableViewController,你有两个选择。使用内置编辑按钮,这将为您提供自动编辑/完成按钮更改。在导航栏中使用self.editButtonItem
:
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
或者您可以使用setEditing:animated
来更改tableViewController的编辑状态:
// self is a UITableViewController
[self setEditing:YES animated:YES];
如果你的viewController不是UITableViewController的子类,你可以设置tableView的编辑模式:
[self.tableView setEditing:YES animated:YES];
如果你有一个UITableViewController你应该使用第一种方式,因为它是最方便的。