对UITableView的一些设置

通过设置UITableView的属性实现cell的移动,删除,编辑

移动cell

-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
//先获取到起始位置的数据
    NSString *str = [self.arr[sourceIndexPath.row]retain];
//    2.把起始位置的对象从数据源中移除
    [self.arr removeObjectAtIndex:sourceIndexPath.row];
//    3.把数据插入到数组的目的地位置上去
    [self.arr insertObject:str atIndex:sourceIndexPath.row];
    [str release];
}

修改删除按钮的标题

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
//返回值就是修改的标题
return @"";
} 

通过左右滑动显示按钮,按钮可以自己编辑

#pragma mark 这个方法是iOS8.0之后出现的方法,可以再编辑状态的时候有多个按钮
-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
//        按钮点击所要触发的事件,都是写在block中
        NSLog(@"触发了删除按钮");
    }];

    UITableViewRowAction *editAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"编辑" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        NSLog(@"触发了编辑按钮");
    }];
    editAction.backgroundColor = [UIColor grayColor];
    deleteAction.backgroundColor = [UIColor purpleColor];
    return @[deleteAction,editAction];
}

删除数据的方法

//删除数据
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
//        先删除数据源
        [self.arr removeObjectAtIndex:indexPath.row];
//        [self.tableView reloadData];
//        通过tableview来删除上面的cell
//        第一个参数:指定删除哪一个分区的那个行,把它作为一个元素放在数组中
//        第二个参数:删除动画
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }
}

实现方法

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}
#pragma mark 重写系统的编辑按钮点击触发的方法
-(void)setEditing:(BOOL)editing animated:(BOOL)animated{
    [super setEditing:editing animated:YES];
    [self.tableView setEditing:editing animated:YES];
}
#pragma mark 设置哪些行可以进行编辑
//-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row % 2 == 0) {
        return NO;
    }
    else{
    return YES;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值