当前位置: 首页 > news >正文

UITableViewCell单元格的删除、插入、移动

UITableViewDelegate的方法

 
   设置编辑模式 中得cell的编辑样式(删除或插入)
     - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
 
UITableViewDataSource的方法
     
     设置该单元格能否被编辑
     - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
     设置该单元格能否被移动
     - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
 
     对单元格进行编辑时会调用此方法
 
删除单元格:1)先删除数据源 2)接着删除单元格 3)刷新单元格
插入单元格:1)先插入数据源 2)接着插入单元格 3)刷新单元格
 
     - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
     //判断编辑样式(删除或插入)
     if (editingStyle==UITableViewCellEditingStyleDelete)  
     {  
      //必须要先删除数据源

          NSMutableArray *arr=[self.dataSourceArray objectAtIndex:indexPath.section];

          [arr removeObjectAtIndex:indexPath.row];

          //接着删除单元格(参数是数组,可能删除多行)
          [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationFade];
          //删除完后要刷新tableView
          [tableView reloadData];
     }
     else if (editingStyle==UITableViewCellEditingStyleInsert)  //插入模式
     {
          //下面根据实际情况
          CSFriends *friend=[[CSFriends alloc]init];
          friend.imageName=[NSString stringWithFormat:@"%d.jpg",2];
          friend.name=@"超级布罗利";
          friend.skill=@"超级赛亚人";
          //必须要先插入数据源
          NSMutableArray *arr=[self.dataSourceArray objectAtIndex:indexPath.section];
          [arr insertObject:friend atIndex:indexPath.row];
          //接着插入单元格(参数是数组,可能插入多行)
          [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
          //插入后要刷新tableView
          [tableView reloadData];
    }
}
 
      对单元格进行移动时会调用此方法
 
移动单元格:1)先将要移动的数据从数据源的原位置中删除 2)接着再讲数据插入数据源的新位置 3)刷新单元格
 
     -(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
     //先找到要移动的数据(sourceIndexPath是移动前的位置)
     NSMutableArray *array=self. dataSourceArray[sourceIndexPath.section];
     CSFriends *friend= array[sourceIndexPath.row];
     //将该数据从数据源删除
     [array removeObject:friend];
     //再找到新位置的数据源(destinationIndexPath是移动后的位置)
     NSMutableArray *array=self. dataSourceArray[destinationIndexPath.section];
     //将数据先添加入数据源的新位置
     [array insertObject:friend atIndex:destinationIndexPath.row];
     //最后刷新单元格
     [tableView reloadData];
}

转载于:https://www.cnblogs.com/caixiang/p/4594736.html

相关文章:

  • 置信区间
  • centos学习(之一)-centos6.6环境搭建
  • android 简单的 左右上下手势 判断
  • 【HM】第1课:HTML+CSS
  • MySQL 备份和恢复
  • python3.4学习笔记(四) 3.x和2.x的区别,持续更新
  • mysql 使用问题记录
  • NYOJ129 决策树 【并检查集合】
  • 关于 android百度地图 调用 地理位置 经纬度坐标,只调用一次的解决方法,通知栏不总是 搜索 GPS 。。。...
  • 最长公共子序列--动态规划算法
  • window.onload 与$(document).ready()的区别
  • 爱上OpenCL的十个理由
  • ionic之angular拦截器
  • 运行时间(Java版本)—转换毫秒到时分秒日期
  • hibernate_使用笔记
  • ----------
  • python3.6+scrapy+mysql 爬虫实战
  • 【跃迁之路】【641天】程序员高效学习方法论探索系列(实验阶段398-2018.11.14)...
  • 11111111
  • android百种动画侧滑库、步骤视图、TextView效果、社交、搜房、K线图等源码
  • Babel配置的不完全指南
  • CSS 专业技巧
  • Java的Interrupt与线程中断
  • Redis 中的布隆过滤器
  • SpringCloud集成分布式事务LCN (一)
  • web标准化(下)
  • 纯 javascript 半自动式下滑一定高度,导航栏固定
  • 计算机在识别图像时“看到”了什么?
  • 简单基于spring的redis配置(单机和集群模式)
  • 七牛云 DV OV EV SSL 证书上线,限时折扣低至 6.75 折!
  • 如何实现 font-size 的响应式
  • 项目管理碎碎念系列之一:干系人管理
  • 小程序上传图片到七牛云(支持多张上传,预览,删除)
  • 一些关于Rust在2019年的思考
  • 用 vue 组件自定义 v-model, 实现一个 Tab 组件。
  • ​LeetCode解法汇总2182. 构造限制重复的字符串
  • ​批处理文件中的errorlevel用法
  • #pragma pack(1)
  • #图像处理
  • $(function(){})与(function($){....})(jQuery)的区别
  • ()、[]、{}、(())、[[]]命令替换
  • (04)odoo视图操作
  • (2)关于RabbitMq 的 Topic Exchange 主题交换机
  • (LeetCode) T14. Longest Common Prefix
  • (pojstep1.3.1)1017(构造法模拟)
  • (Redis使用系列) Springboot 使用redis的List数据结构实现简单的排队功能场景 九
  • (附源码)计算机毕业设计ssm高校《大学语文》课程作业在线管理系统
  • (接口封装)
  • (十三)Java springcloud B2B2C o2o多用户商城 springcloud架构 - SSO单点登录之OAuth2.0 根据token获取用户信息(4)...
  • (一)Kafka 安全之使用 SASL 进行身份验证 —— JAAS 配置、SASL 配置
  • (中等) HDU 4370 0 or 1,建模+Dijkstra。
  • (转)linux下的时间函数使用
  • (转)详解PHP处理密码的几种方式
  • .NET 4.0中使用内存映射文件实现进程通讯
  • .Net core 6.0 升8.0