- (void)viewDidLoad
{
[super viewDidLoad];
arrlist =[NSMutableArray arrayWithObjects:@"老王",@"小王",@"老李",@"小李",@"老张",@"小张",@"许仙",@"法海",@"孽畜",@"小刘",@"老刘",@"老孙",@"小孙",@"老陈", nil];
UIView *VC=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
//VC.backgroundColor=[UIColor cyanColor];
tableView=[[UITableView alloc]initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain];
tableView.delegate=self;
tableView.dataSource=self;
//在导航栏添加右侧按钮
UIBarButtonItem *rightItem=[[UIBarButtonItem alloc]initWithTitle:@"移动" style:UIBarButtonItemStyleBordered target:self action:@selector(toggleMove)];
//添加到导航栏的右侧
self.navigationItem.rightBarButtonItem=rightItem;
[VC addSubview:tableView];
self.view=VC;
// Do any additional setup after loading the view.
}
//动态的切换导航栏右侧按钮文字,并真正的更改单元格的状态
-(void)toggleMove{
//先获取当前表格的编辑状态
[self.tableView setEditing:!self.tableView.editing animated:YES];
//判断,如果是编辑,文字要显示“完成”
if (self.tableView.editing) {
//设置导航栏的右侧按钮的文字
self.navigationItem.rightBarButtonItem.title=@"完成";
}else{
//如果不是编辑状态,显示“移动”
//设置导航栏的右侧按钮的文字
self.navigationItem.rightBarButtonItem.title=@"移动";
}
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [arrlist count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTable=@"SimpletableItem";
UITableViewCell *cell=[self.tableView dequeueReusableCellWithIdentifier:simpleTable];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTable];
}
cell.textLabel.text=[arrlist objectAtIndex:indexPath.row];
return cell;
}
//提交删除的方法
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle==UITableViewCellEditingStyleDelete) {
//进行删除
[self.arrlist removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}else if (editingStyle==UITableViewCellEditingStyleInsert){
//定义要插入的位置的数组
NSArray *insertIndexPaths=[NSArray arrayWithObjects:indexPath, nil];
//数据同步放到数组中
[self.arrlist insertObject:@"insert new Cell" atIndex:indexPath.row];
//添加显示
[self.tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationMiddle];
}
}
// 真是实现可以从fromIndexPath 移动到 toIndexPath;
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
//获取当前行和要移动的行
int fromRow = fromIndexPath.row;
int toRow = toIndexPath.row;
//保存将要移动的数据
id fromObj = [self.listData objectAtIndex:fromRow];
//删除原来的数据
[self.listData removeObjectAtIndex:fromRow];
[listData insertObject:fromObj atIndex:toRow];
// for (NSString *name in listData) {
// NSLog(@"name:%@",name);
// }
}
//让表格可以移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (UITableViewCellEditingStyle)tableView:
(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath*)indexPath {
return UITableViewCellEditingStyleInsert;
}