UITableView中行的操作
iOS开发:UITableView中行的操作
主要讲的表格的操作包括:标记行、移动行、删除行、插入行。
这次就不从头建立工程了,在http://www.oschina.net/code/snippet_164134_9876下载工程。这个工程就是最简单的产生一个表格并向其中写入数据。用Xcode 4.2打开它,在这个工程基础上实现以上操作。
1、标记行
这里讲的标记行指的是单击此行,可以实现在此行右边出现一个勾,如下图所示:
为了实现标记功能,在ViewController.m中@end之前添加代码:
#pragma mark - #pragma mark Table Delegate Methods - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *oneCell = [tableView cellForRowAtIndexPath: indexPath];if (oneCell.accessoryType == UITableViewCellAccessoryNone) {oneCell.accessoryType = UITableViewCellAccessoryCheckmark;} else oneCell.accessoryType = UITableViewCellAccessoryNone;[tableView deselectRowAtIndexPath:indexPath animated:YES]; }
该代码实现:单击某行时,若此行未被标记,则标记此行;若此行已经被标记,则取消标记。
运行效果如上图。
上面的代码实际上就是修改某行的accessoryType属性,这个属性可以设为四个常量:
UITableViewCellAccessoryCheckmark UITableViewCellAccessoryDetailDisclosureButton UITableViewCellAccessoryDisclosureIndicator UITableViewCellAccessoryNone
效果依次如下图所示:
UITableViewCellAccessoryCheckmark UITableViewCellAccessoryDetailDisclosureButton
UITableViewCellAccessoryDisclosureIndicator UITableViewCellAccessoryNone
注意,上面第二张图片中的蓝色圆圈不仅仅是一个图标,还是一个控件,点击它可以触发事件,在上一篇博客《iOS开发16:使用Navigation Controller切换视图》使用过。
2、移动行
想要实现移动或者删除行这样的操作,需要启动表格的编辑模式。使用的是setEditing:animated:方法。
2.1 打开ViewController.xib,将其中的表格控件映射成Outlet到ViewController.h,名称为myTableView。
2.2 打开ViewController.m,在viewDidLoad方法最后添加代码:
//启动表格的编辑模式 [self.myTableView setEditing:YES animated:YES];
2.3 在@end之前添加代码:
//打开编辑模式后,默认情况下每行左边会出现红的删除按钮,这个方法就是关闭这些按钮的 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableVieweditingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleNone; } //这个方法用来告诉表格 这一行是否可以移动 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { return YES; }//这个方法就是执行移动操作的 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {NSUInteger fromRow = [sourceIndexPath row]; NSUInteger toRow = [destinationIndexPath row]; id object = [list objectAtIndex:fromRow]; [list removeObjectAtIndex:fromRow]; [list insertObject:object atIndex:toRow]; }
editingStyleForRowAtIndexPath这个方法中用到了常量UITableViewCellEditingStyleNone,它表示不可编辑,这里的编辑指的是删除和插入。表示表格行的编辑模式的常量有:
UITableViewCellEditingStyleDelete UITableViewCellEditingStyleInsert UITableViewCellEditingStyleNone
顾名思义,第一个表示删除,第二个表示插入,第三个表示不可编辑。
若将editingStyleForRowAtIndexPath方法中的UITableViewCellEditingStyleNone依次换成上面三个值,则它们运行的效果依次如下图所示:
2.4 运行,从下图可以看到实现了行的移动:
但是也会发现,现在无法对每行进行标记了。这说明,在编辑模式下,无法选择行,从而didSelectRowAtIndexPath这个方法不会执行。
3、删除行
从第2步过来,实现删除某行,其实比较简单了。
3.1将editingStyleForRowAtIndexPath方法中的UITableViewCellEditingStyleNone修改成UITableViewCellEditingStyleDelete。
3.2 在@end之前添加代码:
//这个方法根据参数editingStyle是UITableViewCellEditingStyleDelete //还是UITableViewCellEditingStyleDelete执行删除或者插入 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {NSUInteger row = [indexPath row];if (editingStyle == UITableViewCellEditingStyleDelete) {[self.list removeObjectAtIndex:row]; [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationAutomatic]; } }
在这个方法中又出现了一个常量:UITableViewRowAnimationAutomatic,它表示删除时的效果,类似的常量还有:
UITableViewRowAnimationAutomatic UITableViewRowAnimationTop UITableViewRowAnimationBottom UITableViewRowAnimationLeft UITableViewRowAnimationRight UITableViewRowAnimationMiddle UITableViewRowAnimationFade UITableViewRowAnimationNone
它们的效果就不一一介绍了,可以在实际使用时试试。
3.3 运行,看看效果:
刚运行时显示如左边的图片,点击某一行左边的圆圈图标,会显示如中间图片所示。然后点击Delegate按钮,那一行就会被删除掉,如右边的那张图片所示,它显示的是删除时的效果。
4、插入行
这个与删除行类似。
4.1 首先将editingStyleForRowAtIndexPath方法中的UITableViewCellEditingStyleDelete修改成UITableViewCellEditingStyleInsert。
4.2在3.2添加的方法中添加代码:
else {//我们实现的是在所选行的位置插入一行,因此直接使用了参数indexPathNSArray *insertIndexPaths = [NSArray arrayWithObjects:indexPath,nil];//同样,将数据加到list中,用的row[self.list insertObject:@"新添加的行" atIndex:row];[tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationRight]; }
上面的代码中也可以不用insertRowsAtIndexPaths方法,而直接使用[tableView reloadData];语句,但是这样就没有添加的效果了。
4.3 好了,运行一下:
UITableView中行的操作相关推荐
- UITableView 系列四 :项目中行的操作 (添加移动和删除)(实例)
这篇文章主要讲的表格的操作包括:标记行.移动行.删除行.插入行. 这次就不从头建立工程了,在http://dl.iteye.com/topics/download/441cdcca-3191-321b ...
- Hive中行拆分操作
0.背景 在使用Hive的时候有时候会遇到需要将一行"拆分"成多行的操作,如下. 原始数据格式,表名为student_table class student_array 1 [To ...
- WPF DataGrid 如何将被选中行带到视野中
WPF DataGrid 如何将被选中行带到视野中 目录 前言 准备工作 方法一 方法二 总结 独立观察员 2021 年 12 月 11 日 前言 在 WPF 开发中,显示表格一般使用 DataGri ...
- iOS UITableView的使用 (选自oschina)
1.新手篇创建tableView http://my.oschina.net/joanfen/blog/203041 2.进阶篇列表中行的操作 http://my.oschina.net/jo ...
- iOS11、iPhone X、Xcode9 适配指南
2017.09.23 不断完善中... 2017.10.02 新增 iPhone X 适配官方中文文档 更新iOS11后,发现有些地方需要做适配,整理后按照优先级分为以下三类: 单纯升级iOS11后造 ...
- JeecgBoot低代码平台 2.4.5 版本发布,钉钉与企业微信集成版本
项目介绍 JeecgBoot是一款基于代码生成器的低代码平台!前后端分离架构 SpringBoot2.x,SpringCloud,Ant Design&Vue,Mybatis-plus,Shi ...
- mysql 基础命令进阶
文章目录 基础命令回顾 数据类型 常用SELECT命令 导入数据库 导出数据库 扩展知识 SQL查询语句进阶 连接查询: 破解mysql数据库密码 基础命令回顾 添加字段: alter table 表 ...
- Mysql-SQL语句进阶(一)
修改数据表 添加字段: alter table 表名 add 字段名 列类型 [not null|null][primary key][unique][auto_increment][defa ...
- 2-12-mysql-sql语句进阶
回顾前面的基础命令语句 修改数据表 添加字段: alter table 表名 add 字段名 列类型 [not null|null][primary key][unique][auto_increme ...
最新文章
- 数据导出Excel表格
- MySQL · 最佳实践 · 如何索引JSON字段
- JS使用onscroll、scrollTop实现图片懒加载
- 09_ClickHouse,ReplacingMergeTree,案例,根据排序键去重,使用版本参数的去重(学习笔记)
- android8.0更新手机,安卓微信8.0.6正式更新:可发1G大文件、表情互动等多项更新!...
- ES5_03_Object扩展
- 【英语】舞动奇迹--荡漾我心
- 程序员如何避免身体被掏空?
- ubuntu7.10下配置java 6和mysql
- 【简报】帮助开发人员在线了解CSS Filter特性的工具 - CSS FilterLab
- Web接入QQ登陆简单入门操作
- freeswitch cdr mysql_freeswitch XML CDRS
- 雷达图按照权重和排名计算出每项得分,并且按照综合得分排序
- hardfault常见原因_STM32如何查找hardfault原因
- 哲理小故事---理想和现实
- 振动焊机的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告
- Circular reasoning
- 【数据分析师-数据分析项目案例二】泰坦尼克号生还者预测案例
- QQ 简洁模式切换失败解决方法
- Part III.S3. 对方案有偏好的直觉模糊多属性决策方法
热门文章
- 到底什么是standalone模式
- 视觉大模型调研(Survey of Visual Foundation Model)
- 解读:直播星芯片被禁意在打击高仿机
- 数字图像处理实验(六)|图像分割{阈值分割、直方图法、OTUS最大类间方差法(edge、im2dw、imfilter、imresize)、迭代阈值法、点检测}(附matlab实验代码和截图)
- ubuntu18.04安装tenda u6无线网卡驱动
- 数据资源 | 八大板块!数据公开下载渠道
- 阿里云双11的红包 不拼智商都不行
- U盘文件系统,分配单元大小,快速格式化与格式化的不同
- 解决flex布局space-between最后一行布局问题超简单方法
- 小米AI推理框架MACE介绍