iOS 开发之如何编辑tableView上的cell

效果图展示:







plist 文件读取和调用

    path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"DetailData.plist"];

 

    list = [[NSArray arrayWithContentsOfFile:path]mutableCopy];

  

1、添加导航控制器,在导航控制器上添加两个 navigationItem 编辑 和 添加

2、点击编辑的时候调用方法

1⃣️//点击一次开始编辑,点击两次结束编辑

- (void)edit

{

    [myTableView setEditing:!myTableView.isEditing animated:YES];

}

2⃣️编辑tableView 上的cell

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

{

    

//    判断编辑中的样式 是否是 删除的样式

    if (editingStyle == UITableViewCellEditingStyleDelete ) {

        

//        1、删除数据

//        2、更新视图


//        移除数组里面的数据

        [list removeObjectAtIndex:indexPath.row];

//        把移除后的数据 同步到 plist 里面

      BOOL success = [list writeToFile:path atomically:YES];

//        如果数据同步成功 ,就删除数据里面的cell

        if (success) {

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

            

        }    

    }

}

3⃣️、cell之间的移动

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

{

//    如果移动的不是同一个位置 才进行操作

    if (sourceIndexPath.row != destinationIndexPath.row) {

        

//       1 保存 需要移动的数据

        NSDictionary *info = list[sourceIndexPath.row];

//       2、移除 需要移动的数据

        [list removeObjectAtIndex:sourceIndexPath.row];

//       3、插入数据

//        destinationIndexPath 可以得到 咱们要移动的位置

        [list insertObject:info atIndex:destinationIndexPath.row];

//       4、同步数据到plist

        [list writeToFile:path atomically:YES];   

    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值