object-c之UITableView数据的编辑

1.写这个就不写数据了。假设你的都是有数据的了。或者直接弄个可变数组也可当数据源。好就直接进入正题

首先设置编辑模式

//允许他能够编辑
- (BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
//允许排序
- (BOOL) tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
//设置编辑模式,这里我想让页面加载的时候可以删除,然后点击按钮的时候又可以添加数据。所以做了这样的判断
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(!isAdd)
    {
        return UITableViewCellEditingStyleDelete;
    }else
    {
        return UITableViewCellEditingStyleInsert;
    }
}
2.跟新数据包括删除。和添加数据

//改变数据的方法
- (void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(editingStyle==UITableViewCellEditingStyleDelete)
    {
        //删除数据
        //先删除数据数组里面的数据
        [self.array removeObjectAtIndex:indexPath.row];
        //跟新视图
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
    }
    else if(editingStyle==UITableViewCellEditingStyleInsert)
    {
        //插入数据
        //创建弹窗视图
        UIAlertController *alertControll = [UIAlertController alertControllerWithTitle:nil message:@"添加联系人" preferredStyle:UIAlertControllerStyleAlert];
        //创建弹窗按钮
        UIAlertAction *btnCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *btnSure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //封装数据并且添加数据到可变数组Array
            for(UITextField *txt in alertControll.textFields)
            {
                if([txt.text isEqualToString:@""]||txt.text==nil)
                {
                    return ;
                }
            }
            myModel *model = [[myModel alloc]init];
            for(UITextField *txt in alertControll.textFields)
            {
                if(txt.tag==200)
                {
                    model.name = txt.text;
                }
                else if(txt.tag==300)
                {
                    model.tel = txt.text;
                }
            }
            [self.array insertObject:model atIndex:indexPath.row+1];
            //创建一个indexPath,由于我们插入的数据应该在后一位。所以得重新设置一下插入的位置。
            NSIndexPath *myIndex = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:0];
            //视图刷新
            [self.tableView insertRowsAtIndexPaths:@[myIndex] withRowAnimation:UITableViewRowAnimationMiddle];
        }];
        //添加输入框
        [alertControll addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
            textField.placeholder = @"请输入联系人姓名";
            textField.tag = 200;
        }];
        [alertControll addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
            textField.placeholder = @"请输入联系人电话";
            textField.tag = 300;
        }];
        
        [alertControll addAction:btnCancel];
        [alertControll addAction:btnSure];
        //弹出弹出窗
        [self presentViewController:alertControll animated:YES completion:nil];
    }
}
3.改变数据顺序

//改变数据顺序,也就是当你拖动数据再那个位置的时候,然后就改变到你拖动的那个位置
- (void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    //改变数据位置
    if(sourceIndexPath.row<destinationIndexPath.row)
    {
        for(NSInteger i=sourceIndexPath.row;i<destinationIndexPath.row;i++)
        {
            [self.array exchangeObjectAtIndex:i withObjectAtIndex:i+1];
        }
    }
    else
    {
        for(NSInteger i = sourceIndexPath.row;i>destinationIndexPath.row;i--)
        {
            [self.array exchangeObjectAtIndex:i withObjectAtIndex:i-1];
        }
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值