依据http://www.winddisk.com/2012/07/05/uitableview_edit_mod/
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
UITableView *_tableView;
NSMutableArray *_dataArray;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[superviewDidLoad];
self.navigationItem.rightBarButtonItem =self.editButtonItem;
CGRect frame = [UIScreenmainScreen].bounds;
frame.size.height = frame.size.height - 44;
_tableView = [[UITableViewalloc] initWithFrame:framestyle:UITableViewStylePlain];
_tableView.delegate =self;
_tableView.dataSource =self;
[self.viewaddSubview:_tableView];
_dataArray = [[NSMutableArrayalloc] initWithCapacity:20];
for (int i =0; i < 20; i++) {
[_dataArrayaddObject:[NSStringstringWithFormat:@"%d",i]];
}
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[supersetEditing:editing animated:animated];
//1、设置UIViewController的编辑模式时,同时设置tableView的编辑状态,也可以单独设置
[_tableViewsetEditing:editing animated:animated];
}
/*tableView在向自身发送setEditing:animated:消息的前后,会向其delegate分别发送tableView:willBeginEditingRowAtIndexPath: ,tableView:didEndEditingRowAtIndexPath:消息。在这些方法中可相应更新tableView的显示。How does the Twitter iPhone app implement side swiping on a table?中通过实现tableView:willBeginEditingRowAtIndexPath:方法使得用户在tableView的行上swipe时可滑出菜单。
*/
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
//setEditing:animated:前
}
- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
//setEditing:animated:后
}
#pragma mark -tableView delegate and dataSource
#pragma mark @required
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_dataArraycount];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID =@"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellID];
}
cell.textLabel.text = [_dataArrayobjectAtIndex:indexPath.row];
return cell;
}
#pragma mark -tableView delegate and dataSource
#pragma mark @edit method
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
//2、tableView收到setEditing:animated:时,调用此函数,询问哪些cell可编辑,默认实现此方法
if (indexPath.row <=11) {
return YES;
}
return NO;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
//2、tableView收到setEditing:animated:时,调用此函数,询问是否显示移动标志,不实现时默认显示
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
//3、返回具体的编辑模式,若不实现,默认删除模式
if (indexPath.row %2 == 0) {
returnUITableViewCellEditingStyleDelete;
}else{
returnUITableViewCellEditingStyleInsert;
}
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//4、根据具体的编辑模式进行插入删除
if (editingStyle ==UITableViewCellEditingStyleInsert) {
[_dataArrayinsertObject:@"new"atIndex:indexPath.row];
[_tableViewinsertRowsAtIndexPaths:[NSArrayarrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}elseif(editingStyle ==UITableViewCellEditingStyleDelete){
[_dataArrayremoveObjectAtIndex:indexPath.row];
[_tableViewdeleteRowsAtIndexPaths:[NSArrayarrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
//3.用户拖动某行sourceIndexPath经过目标行proposedDestinationIndexPath上方时,调用此函数询问是否可以移动,若不能移动则返回一个新的目的indexPath,否则直接返回proposedDestinationIndexPath,若无特别要求不需要实现
if (proposedDestinationIndexPath.row ==5) {
return [NSIndexPathindexPathForRow:8inSection:0];
}
return proposedDestinationIndexPath;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
//4.实现此方法时,进入编辑模式后右侧会出现移动标志
//根据实际移动情况更改tableView
if (sourceIndexPath == destinationIndexPath) {
return;
}
id object = [_dataArrayobjectAtIndex:sourceIndexPath.row];
[_dataArrayremoveObjectAtIndex:sourceIndexPath.row];
[_dataArrayinsertObject:object atIndex:destinationIndexPath.row];
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
//改变delete按钮上要显示的文字
return@"我要删除你";
}