UITableViewEdit

9 篇文章 0 订阅

- (void)viewDidLoad {

    [super viewDidLoad];

    

    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];

    tableView.tag = 1001;

 

    tableView.delegate = self;

    tableView.dataSource = self;

    

    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCellIdentifier"];

    

    [self.view addSubview:tableView];

    [tableView release];

    

    _dataSoure = [[NSMutableArray alloc] initWithObjects:@"a", @"b", @"c", @"d", nil];

//   系统自带编辑按钮 - 可以直接赋值使用

    self.navigationItem.leftBarButtonItem = self.editButtonItem;

}


//系统自带编辑按钮的点击事件

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {

    [super setEditing:editing animated:animated];

    UITableView *tableView = (UITableView *)[self.view viewWithTag:1001];

//    tableView处于可编辑状态

    [tableView setEditing:editing animated:animated];

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {   

    return _dataSoure.count;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCellIdentifier"];    

    if (_dataSoure.count > indexPath.row) {

        cell.textLabel.text = _dataSoure[indexPath.row];

    }

    return cell;

}


#pragma mark -- UITableViewEdit


//编辑样式

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {

//  分为none\添加\删除    

//    当为第一个cell的时候可编辑属性为添加

    if (indexPath.row == 0) {

        return UITableViewCellEditingStyleInsert;

    }

    return UITableViewCellEditingStyleDelete;

}


//控制是否允许编辑

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

//允许编辑

    return YES;

}



- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

//    当样式为添加时

    if (editingStyle == UITableViewCellEditingStyleInsert) {

        [self.dataSoure addObject:@"��"];

        [tableView reloadData];

    }


//    删除

    if (editingStyle == UITableViewCellEditingStyleDelete) {

//    先处理数据

        [self.dataSoure removeObjectAtIndex:indexPath.row];

//    再刷新tableView

        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];

    }

}


//控制每一行是否可以移动

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

    return YES;

}


//移动完成执行的协议方法

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


    NSString *string = self.dataSoure[sourceIndexPath.row];

    [self.dataSoure removeObjectAtIndex:sourceIndexPath.row];

    [self.dataSoure insertObject:string atIndex:destinationIndexPath.row];

    

    [tableView reloadData];

    

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值