iOS学习之UItableView
一些相关的总结,有点乱.
- UITableView是iOS中提供的用来以列的形式展示数据的视图,叫做表现图,但是只有一列,而且只能在垂直方向滚动.继承自UIScrollView.
- UITableView由多个分区组成(相当于班级的分组),每个分区由多行组成(相当于每个分组下的人).
- UITableView有两种样式,Plain和Group样式,一旦设置之后,后期不能更改.
继承自UITableViewController 与 继承自UIViewController的区别. (UITableViewController是UIViewController的子类)
- .前者根视图是tableView, 而后者根视图是UIView. 前者不需要指定dataSource,delegate.服从协议. 而后者需要.
- 前者不需要重写setEditing:animated:方法控制tableView进入编辑状态,而后者需要自己实现.
- 前者对于UITableViewDataSource协议中的常用方法已经自动生成,而后者需要自己添加对应的方法.
何时需要继承自UITableViewController?
当前页面信息的展示主要是以列的形式来展示的场景下, 都可以直接继承自UITableViewController.
在继承自UITableViewController的视图控制器中访问tableView.
1.self.view 根视图就是tableView.
2.self.tableView 有对应的tableView属性.
UITableView协议中的一些方法
UITableViewDataSource协议
1.配置TableView一共有几个分组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
2.配置tableView每个分区对应的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
3.配置用来显示每一行数据的cell.(UITableViewCell)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {//1.创建重用标识符.static NSString *identifier = @"heihei";//2.根据重用标识符去重用队列中取可重用的cell.UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];//3.判断是否成功取到可重用的cell.cell是否为空.if (!cell) {//4.cell为空,说明没有成功取到cell.则创建一个cell.cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier] autorelease];cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; //辅助视图样式,小箭头
}NSDictionary *dic = self.addressDic[self.sortedKeys[indexPath.section]][indexPath.row];cell.textLabel.text = dic[@"name"];cell.detailTextLabel.text = dic[@"phone"];cell.imageView.image = [[UIImage imageNamed:dic[@"imageName"]] scaleToSize:CGSizeMake(40, 40)];return cell; }
4.配置每个分区的页眉
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
5.配置tableView右侧的分区索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;
//编辑相关的协议方法
6.设置tableView的哪些行可以允许编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
7.提交编辑操作时触发(默认的时删除操作)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
//提交编辑操作, 对删除操作作出处理. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {//总共分两步:1.修改数据源 2.修改界面//1.获取删除行对应的分区key.(是B分组,还是C分组)NSString *key = self.sortedKeys[indexPath.section];//2.根据key获取对应的可变数组.NSMutableArray *group = self.addressDic[key];if (editingStyle == UITableViewCellEditingStyleInsert) {//处理插入操作//1.修改数据源NSDictionary *dic = @{@"name":@"Frank", @"age":@"18", @"gender":@"man", @"phone":@"110", @"imageName":@""};[group insertObject:dic atIndex:indexPath.row];//2.修改界面 [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];} else {//处理删除操作//需要判断是否要删除一个分区.if (group.count == 1) {//删除分区//1.修改数据源//从字典中根据key移除对应的元素. [self.addressDic removeObjectForKey:key];//从排好序的key值数组中移除对应的key. [self.sortedKeys removeObject:key];//2.修改界面NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:indexPath.section];[tableView deleteSections:indexSet withRowAnimation:UITableViewRowAnimationRight];} else {[group removeObjectAtIndex:indexPath.row]; //删除行对应的字典.//删除界面上的一行. [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];}} }
//移动相关的协议方法
8.设置tableView哪些行可以允许移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
9.提交移动操作触发.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath;
//提交移动操作. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {//因为移动操作界面已经发生变化,我们只需要修改数据源即可.//1.获取到分区对应的数组.NSMutableArray *group = self.addressDic[self.sortedKeys[sourceIndexPath.section]];//分区对应的数组//2.将原位置对应的元素取出来保存.NSDictionary *dic = [group[sourceIndexPath.row] retain]; //retain 引用计数加1, 否则移除时就造成引用计数为0,空间回收了.//3.将原位置对应的元素删除掉. [group removeObjectAtIndex:sourceIndexPath.row];//4.将保存的元素插入到目的位置. [group insertObject:dic atIndex:destinationIndexPath.row];//5.释放所有权 [dic release]; }
UITableViewDelegate协议
1.当tableView的行被选中时触发
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
2.当tableView的行被取消选中时触发
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath;
3.配置tableView某一行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
//编辑相关
4.设置tableView的编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
5.设置删除时确认按钮的标题.
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath;
//移动相关
6.设置tableView限制跨区移动
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath;
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath {//sourceIndexPath 移动之前的位置//proposedDestinationIndexPath 即将要移动到的位置if (sourceIndexPath.section == proposedDestinationIndexPath.section) {return proposedDestinationIndexPath;}return sourceIndexPath;}
UITableView编辑步骤:
1.在导航条上添加Edit按钮. 重写setEditing:Animated:方法.
self.navigationItem.rightBarButtonItem = self.editButtonItem;
2.控制tableView的可编辑状态.
3.设置tableView的哪些行可以允许编辑. (dataSource)
4.设置编辑样式. (delegate)
5.提交编辑操作. (dataSource) (1)修改数据源 (2)修改界面
转载于:https://www.cnblogs.com/ErosLii/p/4498881.html
iOS学习之UItableView相关推荐
- iOS开发系列--UITableView全面解析
iOS开发系列--UITableView全面解析 2014-08-23 23:20 by KenshinCui, 2202 阅读, 18 评论, 收藏, 编辑 --UIKit之UITableView ...
- iOS学习资源(一)
用了印象笔记也很久了,这里就把自己整理的一些学习资源分享出来.需要共享印象笔记的小伙伴可以联系: shavekevin@gmail.com 我会把自己的工作笔记共享给大家,和大家一起学习,一起进步. ...
- ios学习--TableView详细解释
2019独角兽企业重金招聘Python工程师标准>>> ios学习--TableView详细解释 分类: ios Object-C 2012-05-17 08:48 1714人阅读 ...
- iOS学习笔记-自己动手写RESideMenu
代码地址如下: http://www.demodashi.com/demo/11683.html 很多app都实现了类似RESideMenu的效果,RESideMenu是Github上面一个stars ...
- flappybird android源码,iOS学习之flappyBird游戏的实现
导言 在本人还是学生的时候,flappyBird这款游戏非常火爆,最后等到Android版的出来之后,也是很痴迷的玩了一把.可是,本人游戏天赋一直平平,几度玩得想摔手机.本文主要介绍如何开发iOS平台 ...
- ios学习 准备列表
2019独角兽企业重金招聘Python工程师标准>>> Skip to content This repository Pull requests Issues Gist Watch ...
- ios学习--iphone开发笔记和技巧总结(原址持续更新)
ios学习--iphone开发笔记和技巧总结(原址持续更新) 分类: ios Object-C2012-04-18 10:16 2716人阅读 评论(1) 收藏 举报 uiviewiphonelist ...
- iOS学习路线图--UIKIt概述
1.先分享一张iOS学习路线图吧,网上找的,感觉不错 2.再来一张UIkit类框架图,也是网上找的 3.UIkit各类概述 1.UIAcceleration: 被叫做加速事件的一个UIAccelera ...
- IOS学习——TableViewController
在接触IOS开发一个星期左右,师傅开始留一些练习作业,其中一个就是比较经典的练习程序--列出系统所有字体,并用相应字体显示,主要是表视图的应用.说到表就不得不说这个TableViewControlle ...
最新文章
- Topcoder SRM 663 DIV 1
- XenDesktop中无法删除虚拟机
- Java程序内存分析
- 考前自学系列·计算机组成原理·中央处理器知识点
- 移动端H5页面注意事项
- php endall(),从G_BEGIN_DECLS和 G_END_DECLS说起
- 对某机构为“转移内部矛盾”而嫁祸于我们的事件之真相大起底
- 无线网络受限制或无连接处理方法
- 【深度学习系列】——深度学习简介
- 如何做相册_我是如何对2000张照片进行批量套版的
- c fun函数求n个整数的平均值_Python语法示例——函数
- 查看Linux系统版本的命令
- 安全优雅的RESTful API签名实现方案
- 第十一个Java程序,计算QQ等级。
- read/write函数
- 计算机如何添加新用户,Win10创建新用户图文教程 Win10怎么新建账户
- 关于redis服务的代码编码
- RF在智能座舱测试中的应用
- DDN周报|3月26日-4月1日
- jenkins api java封装_Jenkins api java 调用
热门文章
- python concat去除重复值语句_Python数据处理从零开始----第二章(pandas)④数据合并和处理重复值...
- python 享元模式_python 设计模式之享元(Flyweight)模式
- python中if有几种使用方式_python 中if-else的多种简洁的写法
- BZOJ 2301 - Problem b(莫比乌斯反演+容斥)
- HDU.3652.B-number(数位DP)
- 通过Servlet的response绘制页面验证码
- 处理SPS错误:只有在配置文件或 Page 指令中将启用会话状态设置为真时,才可以使用会话状态...
- vscode 遇到的迷之bug nvm is not compatible with the npm config prefix
- mongoose日期 时间 范围查询
- python中popen的用法_python中的subprocess.Popen()使用