插入、移动 和删除行 这些都可以通过表视图中setEditing:animated实现。
里面做了一些简单的注释,基本可以理解。
#import "BIDSecondLevelViewController.h"
@interface BIDMoveMeController :BIDSecondLevelViewController
@property (strong,nonatomic)NSMutableArray *list;
-(IBAction)toggleMove;
@end
#import "BIDMoveMeController.h"
@implementation BIDMoveMeController
@synthesize list;
-(IBAction)toggleMove
{
//切换到编辑模式
[self.tableViewsetEditing:!self.tableView.editinganimated:YES];
if(self.tableView.editing)
[self.navigationItem.rightBarButtonItemsetTitle:@"Done"];
else
[self.navigationItem.rightBarButtonItemsetTitle:@"Move"];
}
-(void) viewDidLoad
{
[superviewDidLoad];
//加载数据
if (list==nil) {
NSMutableArray *array= [[NSMutableArrayalloc]initWithObjects:@"Eeny",@"Meeny",@"Miney",@"Mac",@"Catch",@"A",@"Tiger",@"By",@"The",@"Toe",nil];
self.list=array;
}
//创建一个按钮栏项目设置标题为Move style需要一个标准的右边栏 target和action告知单击按钮做什么
UIBarButtonItem *moveButton =[[UIBarButtonItemalloc]initWithTitle:@"Move"style:UIBarButtonItemStyleBorderedtarget:selfaction:@selector(toggleMove)];
self.navigationItem.rightBarButtonItem = moveButton;
}
#pragma mark -
#pragma mark Table Data Source Methods
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [listcount];
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
staticNSString *MoveMeCellIdentifier =@"MoveMeCellIdentifier";
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:MoveMeCellIdentifier];
if (cell==nil) {
cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:MoveMeCellIdentifier];
cell.showsReorderControl=YES;//不会真正显示重新排序控件,除非表进入编辑模式
}
NSUInteger row = [indexPathrow];
cell.textLabel.text=[listobjectAtIndex:row];
return cell;
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
//通过该方法表视图可以询问指定行是否可以被删除,或者是否可以将新行插入到指定位置。
returnUITableViewCellEditingStyleNone;//表示我们不支持插入或删除任何行
}
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
//每一行都将调用该方法,可以通过它禁止移动指定行。
return YES;
}
-(void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
//当移动一行时真正调用的方法两个参数分别只当被移动行和行的新位置。
NSUInteger fromRow = [fromIndexPathrow];//需要移动行的新位置
NSUInteger toRow = [toIndexPathrow];//检索新的位置
id object =[listobjectAtIndex:fromRow];
[list removeObjectAtIndex:fromRow];//数组中移除指定的对象
[list insertObject:objectatIndex:toRow];//指定到新的位置
}
@end
#import "BIDSecondLevelViewController.h"
@interface BIDDeleteMeController :BIDSecondLevelViewController
@property (strong,nonatomic)NSMutableArray *list;
-(IBAction)toggleEdit:(id)sender;
@end
#import "BIDDeleteMeController.h"
@implementation BIDDeleteMeController
@synthesize list;
-(IBAction)toggleEdit:(id)sender
{
[self.tableViewsetEditing:!self.tableView.editinganimated:YES];
if (self.tableView.editing) {
[self.navigationItem.rightBarButtonItemsetTitle:@"Done"];
}
else
[self.navigationItem.rightBarButtonItemsetTitle:@"Delete"];
}
-(void)viewDidLoad
{
[superviewDidLoad];
if (list==nil) {
NSString *path = [[NSBundlemainBundle]pathForResource:@"computers"ofType:@"plist"];
NSMutableArray *array= [[NSMutableArrayalloc] initWithContentsOfFile:path];
self.list=array;
}
UIBarButtonItem *editButton = [[UIBarButtonItemalloc]initWithTitle:@"Delete"style:UIBarButtonItemStyleBorderedtarget:selfaction:@selector(toggleEdit:)];
self.navigationItem .rightBarButtonItem= editButton;
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [listcount];
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *DeleteMeCellIdentifier=@"DeleteMeCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DeleteMeCellIdentifier];
if (cell==nil) {
cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:DeleteMeCellIdentifier];
}
NSInteger row =[indexPath row];
cell.textLabel.text=[self.listobjectAtIndex:row];
return cell;
}
#pragma mark -
#pragma mark Table Data Source Methods
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//当用户完成一项编辑(插入或删除)操作时,表视图调用该方法
NSInteger row =[indexPath row];
[self.listremoveObjectAtIndex:row];
[tableView deleteRowsAtIndexPaths:[NSArrayarrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
@end