CYC- tableView的删除编辑移动

#import "RootViewController.h"
@interface RootViewController ()
@property (nonatomic, retain)NSMutableArray *firstArray;
@property (nonatomic, retain)NSMutableArray *secondArray;
@property (nonatomic, retain) UITableView *tableView;
@end
@implementation RootViewController

记得释放

- (void)dealloc
{
    [_firstArray release];
    [_secondArray release];
    [_tableView release];
    [super dealloc];
}

别忘记调用方法


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self setUpData];
    [self addTableView];
    [self addBarButtonItem];

}

进入正题

// 添加数据源
- (void)setUpData
{
    NSArray *tempArray = @[@"0",@"1",@"2",@"3",@"添加"];
    // 初始化 两个可变数组源数组
    self.firstArray = [NSMutableArray arrayWithArray:tempArray];
    self.secondArray = [NSMutableArray arrayWithArray:tempArray];
}


#pragma mark - 创建tableView
- (void)addTableView
{

    self.tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStylePlain)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [_tableView release];
}


#pragma mark - 两个重要的数组源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 0) {
        return self.firstArray.count;
    }else {
    return self.secondArray.count;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"MyCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier ];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier] autorelease];
    }

    // 显示数据 先判断分区
    // 再用行数 作为数组的索引 取出数据

    if (indexPath.section == 0) {
        cell.textLabel.text = self.firstArray[indexPath.row];
    }else {
        cell.textLabel.text = self.secondArray[indexPath.row];
    }


    return cell;
}


#pragma mark - 返回分区数
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return @"第一分区";
    }else {
        return @"第二分区";
    }
}
#pragma mark - 添加右按钮
- (void)addBarButtonItem
{
   self.navigationItem.title = @"主界面";
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]initWithTitle:@"编辑" style:(UIBarButtonItemStylePlain) target:self action:@selector(actionRightButton:)];
    self.navigationItem.rightBarButtonItem = rightButton;
    [rightButton release];
}

UITableView 编辑两步取:
1. 对UI界面进行操作
2. 对数据源数据进行操作 (防止刷新界面后 界面没有变化)
UITableView编辑步骤:
1. 让UITableView成为 可编辑状态
2. 返回(指定)可以被编辑的分区的那行 默认是都能编辑
3. 返回(指定)那个分区或那行的编辑样式 添加or删除
4. 完成编辑 根据编辑样式 完成不同的操作 操作大体分两步: (1)更改数据源数据 (2)刷新页面

// 编辑触发按钮方法
// 1.开启编辑状态
- (void)actionRightButton:(UIBarButtonItem *)rightButton
{
    // 开启 UITableView 编辑状态
    // self.tableView.editing 默认是NO的

    [self.tableView setEditing:!self.tableView.editing animated:YES];

    // 更改编辑按钮标题
    if (self.tableView.editing == YES) {
        rightButton.title = @"完成";
       // self.navigationItem.rightBarButtonItem.title = @"完成";
    }else {
         rightButton.title = @"编辑";
        // self.navigationItem.rightBarButtonItem.title = @"编辑";
    }
}
// 2.哪个indexPath(那个分区的那行) 可以被编辑
// 如果你不写 默认是YES的

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
//    if (indexPath.section == 0 && indexPath.row == 1) {
//        return NO;
//    }
    return YES;
}
// 3. 指定被编辑的样式 添加or删除
// 返回哪个分区的那行的 编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 判断分区
    if (indexPath.section == 0) {
        // 第一分区
        // 再判断数组中的数据, 只让添加字样 返回添加
        if([self.firstArray[indexPath.row] isEqualToString:@"添加"]) {
            return UITableViewCellEditingStyleInsert;
        }

    }else {
        // 第二分区
        if ([self.secondArray[indexPath.row] isEqualToString:@"添加"]) {
            return UITableViewCellEditingStyleInsert;
        }
    }

       return UITableViewCellEditingStyleDelete;
}


// 4. 按编辑样式 提交那个分区的那行的结果 完成编辑
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 更新数据 刷新界面

    // 先判断分区
    if (indexPath.section == 0) {
        // 第一分区
        // 在判断编译样式
        if (editingStyle == UITableViewCellEditingStyleDelete) {
            // 删除
            // 利用indexPath.row 删除数组中想对应的数据
            [self.firstArray removeObjectAtIndex:indexPath.row];

            // 刷新界面
            // 下面方法 用在删除数据刷新界面
            // 需要一个数组 数组中是删除的索引
            // 这个数组 可以是多行的索引
            [self.tableView deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:(UITableViewRowAnimationRight)];

    } else {

        // 添加
        // 更改数据
        [self.firstArray insertObject:@"123" atIndex:indexPath.row];
        // 刷新界面
        // 下面方法 用在插入数据时 刷新界面
        // 这个方法 是对行进行的刷新
        [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationRight)];
       }

    }else{
        // 第二分区
        if (editingStyle == UITableViewCellEditingStyleDelete) {
            [self.secondArray removeObjectAtIndex:indexPath.row];
            [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationRight)];
        }else {
            [self.secondArray insertObject:@"234" atIndex:indexPath.row];
            [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationRight)];
        }
    }
}

UITableView 移动步骤
1.让UITableView成为 可编辑状态
2.指定 那个分区的那行 可以被移动
3.完成移动后 更新数据 刷新界面

// 1.UITableView 可编辑状态(已完成)
// 2.指定 那行那分区 可编辑

-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
//    if (indexPath.section == 0) {
//        return NO;
//    }
    return YES;
}

#pragma mark - 移动完成

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    // sourceIndexPath 来源的索引(你开始拿起来cell的位置)
    // destinationIndexPath 目的地的索引 (放下的cell的位置)

    // 分两种情况
    // 1.同区移动 2.跨区移动

    if (sourceIndexPath.section == destinationIndexPath.section) {
        // 同区
        if(sourceIndexPath.section == 0) {
           // NSLog(@"%ld %ld", sourceIndexPath.row, destinationIndexPath.row);

            // 操作第一分区数组
            // 先保存一下 来源索引处的数据

            NSString *str = self.firstArray [sourceIndexPath.row];

            // 在从数组中 按来源索引删除该数据

            [self.firstArray removeObjectAtIndex:sourceIndexPath.row];

               // 最后 把保存的数据 插入到目的地的地索引处
            [self.firstArray insertObject:str atIndex:destinationIndexPath.row];
            // 刷新界面(移动刷新方法 从那来的去那)
            [self.tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];

        }else {
            //操作第二区分组
            NSString *str = self.secondArray [sourceIndexPath.row];

            // 在从数组中 按来源索引删除该数据

            [self.secondArray removeObjectAtIndex:sourceIndexPath.row];

            // 最后 把保存的数据 插入到目的地的地索引处
            [self.secondArray insertObject:str atIndex:destinationIndexPath.row];
            // 刷新界面(移动刷新方法 从那来的去那)
            [self.tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];

        }

限制跨区移动

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
    // sourceIndexPath 来源索引
    // proposedDestinationIndexPath 推荐的目的地索引

    // 只要你拖动 就会触发这个方法
    if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
        return proposedDestinationIndexPath;
    }else {
        return sourceIndexPath;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值