tableView编辑
1、让tableView处于编辑状态
2、指定tableView哪些行可以编辑
3.指定tableView编辑的样式(添加、删除)
4、编辑完成(先操作数据源,再修改UI)
tableView移动
1、让tableView处于编辑状态
2、指定tableView哪些行可以移动
3.移动完成
4.监测移动过程,实现限制跨区移动
⽆论编辑还是移动,都先让tableView进入编辑状态。 编辑结束或者移动结束,要先修改数组或字典中的数据,在更改UI。
UITableViewController是封装好了各种delegate和datasource,能 提高我们开发速度
<span style="font-size:18px;color:#330033;">#import "RootViewController.h"
#import "RootView.h"
@interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>
{
UITableViewCellEditingStyle _style;
}
@property(nonatomic,strong)RootView *myview;
@property(nonatomic,strong)NSMutableArray *groupArray;
@end
@implementation RootViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
self.myview = [[RootView alloc] initWithFrame:[UIScreen mainScreen].bounds];
}
return self;
}
-(void)loadView
{
self.view = self.myview;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.groupArray = [NSMutableArray array];
//设置代理
self.myview.TableView.dataSource = self;
//设置代理
self.myview.TableView.delegate = self;
[self addAction];
}
-(void)addAction
{ //创建一个barbutton按钮,(删除按钮)
UIBarButtonItem *deleteButton = [[UIBarButtonItem alloc] initWithTitle:@"删除" style:(UIBarButtonItemStyleDone) target:self action:@selector(daleteButton:)];
self.navigationItem.leftBarButtonItem = deleteButton;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:@"添加" style:(UIBarButtonItemStyleDone) target:self action:@selector(addButton:)];
self.navigationItem.rightBarButtonItem = addButton;
//创建一个假数据
for(int i = 0;i < 3;i++)
{
NSMutableArray *tempArray = [NSMutableArray array];
for(int j = 0;j < 5;j++)
{
[tempArray addObject:[NSString stringWithFormat:@"第%d组,第%d个人",i,j]];
}
[self.groupArray addObject:tempArray];
}
// NSLog(@"%@",self.groupArray[0]);
}
//编辑状态
//删除按钮
-(void)daleteButton:(UIBarButtonItem *)sender
{
//当点击"删除"按钮的时候,触发事件,给他一个编辑状态(删除状态)
_style = UITableViewCellEditingStyleDelete;
//一个BOOL 接收此时的编辑状态
BOOL flag = self.myview.TableView.editing;
//设置编辑状态
[self.myview.TableView setEditing:!flag animated:YES];
}
//添加按钮
-(void)addButton:(UIBarButtonItem *)sender
{
_style = UITableViewCellEditingStyleInsert;
BOOL flag = self.myview.TableView.editing;
[self.myview.TableView setEditing:!flag animated:YES];
}
/*-----------------------UITableViewDataSource-----------------------------*/
//下面的方法都是遵循UITableViewDataSource协议的,所以先在viewDidLoad先去设置代理
//设置几个分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.groupArray.count;
}
//设置一个分区里面有几行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.groupArray[section] count];
}
//设置cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//标识
static NSString *cell_id = @"cell_id";
//创建cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_id];
//判断在重用池中有没有cell,如果没有就创建
if (nil == cell)
{
cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:cell_id];
}
//给cell设置标题
cell.textLabel.text = self.groupArray[indexPath.section][indexPath.row];
//给cell设置副标题
cell.detailTextLabel.text = @"我中奖了";
return cell;
}
//指定哪些行可以被编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
//例如让单行处于被编辑状态
if(indexPath.row % 2)
{
return NO;
}
return YES;
}
//编辑样式(添加,删除)
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return _style;
}
//编辑完成
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//当编辑状态为添加状态的时候
if(editingStyle == UITableViewCellEditingStyleDelete)
{
//删除(确定哪个分区的哪一行)
[self.groupArray[indexPath.section]removeObjectAtIndex:indexPath.row];
//刷新数据
[tableView reloadData];
//刷新数据动画
// [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationFade)];
}
else if (editingStyle == UITableViewCellEditingStyleInsert)
{
NSString *string = @"测试数据";
//修改数据源(实现添加)
[self.groupArray[indexPath.section]insertObject:string atIndex:indexPath.row+1];
//修改UI
// [tableView reloadData];
// [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationTop)];
//添加数据到显示到添加行下面
NSIndexPath *indes = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];
//刷新数据带动画效果
[tableView insertRowsAtIndexPaths:@[indes] withRowAnimation:(UITableViewRowAnimationTop)];
}
}
/*-------------------------------tableView移动----------------------------------------*/
//指定哪些被移动
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
//移动完成
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
//记录
id obj = self.groupArray[sourceIndexPath.section][sourceIndexPath.row];
//移除
[self.groupArray[sourceIndexPath.section]removeObjectAtIndex:sourceIndexPath.row];
//添加
[self.groupArray[destinationIndexPath.section]insertObject:obj atIndex:destinationIndexPath.row];
//更新数据源
[tableView reloadData];
}
//限制移动区域
-(NSIndexPath*)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{ //先判断源和目的的分组是否相同,
if (sourceIndexPath.section != proposedDestinationIndexPath.section)
{
//如果不同,返回源的起始位置
return sourceIndexPath;
}
else
{
//如果相同,返回目的的位置
return proposedDestinationIndexPath;
}
}
</span>