单元格的编辑

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *_tableView;
    NSMutableArray *_dataList;
}

@end

@implementation ViewController
//编辑多选模式
/* 首先在我们的头视图上添加一个 button ,用于控制 tableView 是否处于编辑状态 设置哪些单元格可以编辑哪些不可以,当设置编辑状态完成后默认所有单元格都可以编辑 设置每个单元格编辑模式下得按钮样式 (新增和删除),默认是新增 在 commitEditing 方法里增加或者减少,要使用_ dataList使用 reloadData 或者增加 cell的方法实现 设置可移动单元格 移动的位置调整,先取下来,删除原有位置 ,增加插入的新位置 section 不需要设置宽度,组不需要设置组高 */
- (void)viewDidLoad {
    [super viewDidLoad];
    //创建一个 UITableView
    _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];

    //签代理
    _tableView.delegate=self;
    _tableView.dataSource=self;

    [self.view addSubview:_tableView];
    //编写一个数据
    _dataList=[NSMutableArray arrayWithArray:@[@"zhangSan",@"liSi",@"wangWu",@"niuniu"]];

    UIView *headView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 100)];
    UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-80, 10, 60, 30)];
    [headView addSubview:button];
    [button setTitle:@"编辑" forState: UIControlStateNormal];
    [button setTitle:@"完成" forState: UIControlStateSelected];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

    _tableView.tableHeaderView=headView;
    _tableView.tableHeaderView.backgroundColor=RGBA(150, 200, 200, 50);

}
-(void)buttonAction:(UIButton *)button{
    button.selected=!button.selected;

    _tableView.editing=!_tableView.editing;

    if (!_tableView.editing) {
        [_tableView reloadData];
    }
}

-(NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataList.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];

    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.textLabel.text=_dataList[indexPath.row];

    return cell;
}
    //这个方法是设置某个单元格可不可以被编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.row==0) {
        return NO;
    }
    return YES;
}
    //设置编辑模式下得按钮样式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{


    if (indexPath.row==1) {
        return UITableViewCellEditingStyleInsert;
    }

    return UITableViewCellEditingStyleDelete;
}

    //按照我们的点击,分别实现新增删除的方法
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

    if (editingStyle==UITableViewCellEditingStyleInsert) {
        //插入后要保存数据
        //这里的插入是在点击单元格后面插入
        //_dataList 要用 mutable 是因为这里需要改变他的内容
        [_dataList insertObject:@"guanGuan" atIndex:indexPath.row+1];

        //刷新数据
        //[_tableView reloadData];
        //插入一个 cell 传入一个 array
        //构造新的位置 引用索引 NSIndexPath

        NSIndexPath *indexP=[NSIndexPath indexPathForItem:indexPath.row+1 inSection:indexPath.section];


        NSArray *array=@[indexP];

        [_tableView insertRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationAutomatic];

    }
    else if (editingStyle ==UITableViewCellEditingStyleDelete){

        //先删除数据
        [_dataList removeObjectAtIndex:indexPath.row];
        //再删除单元格
        [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];


    }
}

-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath==0) {
        return NO;
    }
    return YES;
}

-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{

    //拷贝我们要移动的数据
    NSString *moveIten=[_dataList objectAtIndex:sourceIndexPath.row];

    //删除这个数据
    [_dataList removeObjectAtIndex:sourceIndexPath.row];

    //将拷贝好的数据插入要放入的地方
    [_dataList insertObject:moveIten atIndex:destinationIndexPath.row];

    [tableView reloadData];

}



@end

你可能感兴趣的:(单元格)