iOS 学习笔记 uitableView
2016-09-19 15:15
337 查看
一、UITableView基本介绍
默认的UITableView有2种风格:
UITableViewStylePlain(不分组)
UITableViewStyleGrouped(分组)
UITableView中的数据只有行的概念,没有列的概念,UITableView的每行数据就是一个
UITableViewCell。
自带的
UITableViewCell的类型选择有:
typedef NS_ENUM(NSInteger, UITableViewCellStyle) { UITableViewCellStyleDefault, // 左侧显示textLabel(不显示detailTextLabel),imageView可选(显示在最左边) UITableViewCellStyleValue1, // 左侧显示textLabel、右侧显示detailTextLabel(默认蓝色),imageView可选(显示在最左边) UITableViewCellStyleValue2, // 左侧依次显示textLabel(默认蓝色)和detailTextLabel,imageView可选(显示在最左边) UITableViewCellStyleSubtitle // 左上方显示textLabel,左下方显示detailTextLabel(默认灰色),imageView可选(显示在最左边) };
二、UITableViewDataSource数据源
数据源的作用就是告诉UITableView,我该显示什么数据
#pragma mark 常用数据源方法 #pragma mark 返回分组数 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView #pragma mark 返回每组行数 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section #pragma mark 返回每行的单元格 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath #pragma mark 返回每组头标题名称 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section #pragma mark 返回每组尾部说明 - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
计算分组数 -> 计算每组行数 -> 生成分组索引 -> 生成单元格
注意:cellForRowAtIndexPath只生产当前显示在界面上的单元格
三、UITableViewDelegate代理
代理的作用是告诉UITableView,我该怎么显示和响应
#pragma mark - 常用代理方法 #pragma mark 设置分组头部的内容高度 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section #pragma mark 设置每行高度(每行高度可以不一样) - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath #pragma mark 设置分组尾部的内容高度 - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section #pragma mark 点击了某一行 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath #pragma mark 设置分组的头部视图 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section; #pragma mark 设置分组的尾部视图 - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
四、UITableView刷新列表方法
#pragma mark 刷新整个表格 - (void)reloadData; #pragma mark 刷新指定的行 - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation; #pragma mark 刷新指定的分组 - (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation; #pragma mark 删除时刷新指定的行数据 - (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation; #pragma mark 添加时刷新指定的行数据 - (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
五、UITableViewCell的重用机制
在UITableView内部有一个缓存池,专门用来缓存
UITableViewCell,因为
UITableView不是一下子显示全部Cell,而是以 所见即所得 的方式,手机上看的见的Cell,才有存在的对象
UITableViewCell实例。具体表现如下:
每次显示新的Cell的时候,都是先从缓存池中取出对应的
UITableViewCell对象,进行 重新初始化 显示。如果缓存池中没有,才创建新的
UITableViewCell对象
每当某个Cell被移出 可见区域 外后,就会被 回收 到缓存池中
所以尽管要展示的数据巨大,但内存中存在的
UITableViewCell也是有限的,极大的降低了对内存的需求。
# pragma mark - 在tableView:cellForRowAtIndexPath:方法中使用UITableView的重用机制 // 由于此方法调用十分频繁,cell的标示声明成静态变量有利于性能优化 static NSString *cellIdentifier = @"UITableViewCellIdentifierKey1"; // 首先根据标识去缓存池取 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; // 如果缓存池没有找到,则重新创建并放到缓存池中 if(!cell){ cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier]; }
注意:
因为使用了重用机制,你拿到的Cell上面也许本来就有数据,所以一定要对Cell里的所有子控件进行初始化,否则会导致Cell数据混乱。
比如你第一个Cell设置了标题,其他Cell都没有设置标题,你向下刷新的时候,会发现总有一个Cell有标题,但这个Cell不是第一个Cell,你顿时就懵逼了。所以Cell的重用时需要时刻注意初始化的问题。
六、系统自带的UITableViewCell
系统自带的UITableViewCell内部控件示意图
我们基本上很少使用系统自带的
UITableViewCell,样式太过于死板了。
七、自定义Cell
基本步骤:
自定义类
XXXTableViewCell,继承
UITableViewCell
重写
-(id)initWithStyle:reuseIdentifier:方法,添加子控件
最好重写
layoutSubView方法,设置子控件
frame
然后在
UITableView的代理方法
tableView:cellForRowAtIndexPath:中使用重用机制创建该类
XXXTableViewCell,再对cell进行初始化
微博Cell子控件示意图
八、MVC模式
MVC模式示意图
相关文章推荐
- iOS学习笔记——UITableView
- iOS学习笔记--03 UITableView相关
- IOS开发学习笔记042-UITableView总结2
- ios学习笔记UITableViewCell重用时出现重影的解决方案
- iOS学习笔记-099.彩票14——设置3_UITableViewController基类提取
- iOS学习笔记03-UITableView
- iOS学习笔记之UITableViewController&UITableView
- iOS学习笔记1—UITableViewCell设置
- iOS学习笔记-098.彩票13——设置2_UITableViewCell创建
- iOS开发学习笔记——UITableview
- iOS开发学习笔记 UITableview
- iOS学习笔记03-UITableView
- iOS学习笔记之UI-UISearchController-And-UITableView
- 【IOS学习笔记】UITableView隐藏多余分割线
- 黑马程序员-IOS学习笔记-UITableView1
- 新浪微博iOS版SDK“宝玉XP”框架学习笔记
- ios 开发学习笔记(一)
- IOS 学习笔记
- 关于ios新浪sdk学习笔记
- [iOS]学习笔记3(动态性)