[self.navigationItem setRightBarButtonItem:[self editButtonItem]];
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[tableView setEditing:editing animated:animated];
}
TestTableViewController.m
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
}
也可以直接设定rootViewController可编辑
RootViewController.m
[self
设置tableView可编辑的行
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
//设置可移动标志,操作每个cell是否可被移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == [_books count])
{
return NO;
}
return YES;
}
增加或删除
- (UITableViewCellEditingSt
{
if(indexPath.row == [_books count])
{
return UITableViewCellEditingSt
}
return UITableViewCellEditingSt
}
完成编辑
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingSt
{
if(editingStyle == UITableViewCellEditingSt
{
//插入一条新条目的时候,会更新numberOfRowsInSection 方法,并且 运行一次cellForRowAtIndexPath,生成一个新增的cell
Book *book = [[Book alloc] initWithISBN:@"999" name:@"New Book" cover:nil];
[_books insertObject:book atIndex:indexPath.row];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationR
}
else if(editingStyle == UITableViewCellEditingSt
{
//删除一条条目时,更新numberOfRowsInSection
[_books removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationB
}
}