UITableView

介绍

  • UITableView 是用来用列表的形式显示数据的UI控件

    • 举例
    • QQ好友列表
    • 通讯录
    • iPhone设置列表

tableView 常见属性

    // 设置每一行cell的高度
self.tableView.rowHeight = 100; // 设置每一组头部的高度
self.tableView.sectionHeaderHeight = 50; // 设置每一组尾部的高度
// self.tableView.sectionFooterHeight = 50; // 设置分割线颜色
self.tableView.separatorColor = [UIColor redColor];
// 设置分割线样式
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
// 设置表头控件
self.tableView.tableHeaderView = [[UISwitch alloc] init];
// 设置表尾控件
self.tableView.tableFooterView = [UIButton buttonWithType:UIButtonTypeContactAdd];

UITableView的两种样式只读属性

  • 只读属性,在代码中不能修改

    UITableViewStylePlain
  • 一组显示Section = 1;
UITableViewStyleGrouped
  • 分组显示Section >= 1;

展示数据

遵守协议
  • UITableViewDataSource
设置数据源
  • 连线
  • 代码
实现数据源方法
  • 先调多少组
//调用数据源的下面方法得知一共有多少组数据
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
  • 多少行
//调用数据源的下面方法得知每一组有多少行数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- 每行数据
//调用数据源的下面方法得知每一行显示什么内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

UITableView右边的索引条

  • 属性

//设置tableView右边索引文字的颜色
self.tableView.sectionIndexColor = [UIColor redColor];
//设置右边索引文字背景的颜色
self.tableView.sectionIndexBackgroundColor = [UIColor grayColor]; //数据源方法索引信息
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
//返回的是一个数组,数组中的元素是显示信息,只是提示,结果还是按索引位置分组
return [NSArray arrayWithObjects:@"a",@"b",@"c",@"d", nil];
}

tableView常用方法

//设置分组的头部数据
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return @"头部";
}
//设置分组的尾部数据
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
return @"头部";
}
**
* 当选中一行的时候调用(点击)
*/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// XMGWine *wine = self.wineArray[indexPath.row];
// NSLog(@"点击了:%@", wine.name);
NSLog(@"选中了:%zd", indexPath.row);
} /**
* 当取消选中一行的时候调用
*/
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"取消选中了:%zd", indexPath.row);
}
/**
* 返回每个cell的高度
*/
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row % 2 == 0) {
return 50;
} else {
return 100;
}
}

UITableViewCell

  • cell 的样式属性

    • UITableViewCellStyleDefault
    • UITableViewCellStyleValue1
    • UITableViewCellStyleValue2
    • UITableViewCellStyleSubtitle
  • cell 右边指示样式的属性accessoryView

    • 优先级高于accessoryType
  • cell 右边指示样式的属性 accessoryType

    • UITableViewCellAccessoryNone
    • UITableViewCellAccessoryDisclosureIndicator
    • UITableViewCellAccessoryDetailDisclosureButton
    • UITableViewCellAccessoryCheckmark
    • UITableViewCellAccessoryDetailButton
  • cell 被点击的颜色变化iOS 7 之前的

    • UITableViewCellSelectionStyleNone
    • UITableViewCellSelectionStyleBlue
    • UITableViewCellSelectionStyleGray
    • UITableViewCellSelectionStyleDefault

创建 Cell 的性能分析及优化

cell的重用原理

  • iOS设备的内存有限,如果用UITableView显示成千上万条数据,就需要成千上万个UITableViewCell对象的话,那将会耗尽iOS设备的内存。要解决该问题,需要重用UITableViewCell对象

    • 重用原理:当滚动列表时,部分UITableViewCell会移出窗口,UITableView会将窗口外的UITableViewCell放入一个对象池中,等待重用。当UITableView要求dataSource返回UITableViewCell时,dataSource会先查看这个对象池,如果池中有未使用的UITableViewCell,dataSource会用新的数据配置这个UITableViewCell,然后返回给UITableView,重新显示到窗口中,从而避免创建新对象
  • 还有一个非常重要的问题:有时候需要自定义UITableViewCell(用一个子类继承UITableViewCell),而且每一行用的不一定是同一种UITableViewCell,所以一个UITableView可能拥有不同类型的UITableViewCell,对象池中也会有很多不同类型的UITableViewCell,那么UITableView在重用UITableViewCell时可能会得到错误类型的UITableViewCell

    • 解决方案:UITableViewCell有个NSString *reuseIdentifier属性,可以在初始化UITableViewCell的时候传入一个特定的字符串标识来设置reuseIdentifier(一般用UITableViewCell的类名)。当UITableView要求dataSource返回UITableViewCell时,先通过一个字符串标识到对象池中查找对应类型的UITableViewCell对象,如果有,就重用,如果没有,就传入这个字符串标识来初始化一个UITableViewCell对象

不优化时内存分析

不优化的时候

  • 离开可视范围就销毁

    • 频繁的开辟内存,销毁内存
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] init]; cell.textLabel.text = [NSString stringWithFormat:@"%zd行",indexPath.row];
NSLog(@"cellForRowIndexPath --%zd",indexPath.row);
return cell;
}

优化后内存地址分析

Cell的重用代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1.定义一个cell的标识
//static 定义变量----只初始换一次
static NSString *ID = @"jrcell"; // 2.从缓存池中取出cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; // 3.如果缓存池中没有cell
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
//尽量将cell的初始化设置,放在这个代码块中
//如果这个设置是所有cell都要保持一致的,就可以放在这个代码块中
cell.textLabel.font = [UIFont systemFontOfSize:30];
} // 4.设置cell的属性... return cell;
}

cell的重用代码新写法

iOS-UI控件之UITableView(一)的更多相关文章

  1. iOS UI控件继承关系图

    闲来无事,把UI控件的继承关系图整理下来,供自己和大家使用.

  2. ios UI控件的简单整理

    把该文件拷贝到.m文件中就能够方便的查找 /** 匿名类目:能够声明方法和变量,属性为private(不同意在外部调用,且不能被继承 */ /** 发送数据的托付方,接收数据的时代理发(即代理的反向传 ...

  3. iOS UI控件总结(全)

    1.UIButton UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btn.frame = CGRectMake ...

  4. UI控件之UITableView的基本属性

    UITableView:特殊的滚动视图,横向固定,可以在纵向上滚动,自动计算contentSize 创建tableView,初始化时指定样式,默认是plain UITableView *_tableV ...

  5. UI控件之UITableView的协议方法

    <UITableViewDataSource,UITableViewDelegate> //设置表视图的编辑状态 -(void)setEditing:(BOOL)editing anima ...

  6. iOS UI控件

    创建: 2018/04/21 完成: 2018/04/25 更新: 2018/09/24 补充UIActivityIndicatorView的显示和隐藏方法 UIButton  设定项目  项目名   ...

  7. iOS UI控件之间的关系图

  8. IOS学习资源收集--开发UI控件相关

    收集的一些本人了解过的iOS开发UI控件相关的代码资源(本文持续补充更新) 内容大纲: 1.本人在github上也上传了我分装好的一些可重复利用的UI控件 2.计时相关的自定义UILabel控件 正文 ...

  9. ios 中的UI控件学习总结(1)

    UIKit框架提供了非常多功能强大又易用的UI控件 下面列举一些在开发中可能用得上的UI控件 UIButton 按钮 UILabel 文本标签 UITextField 文本输入框 UIImageVie ...

  10. iOS 使用UI控件的外观协议UIAppearance进行设置默认UI控件样式

    在iOS开发中,经常会对UINavigationBar的样式进行全局样式.采用的设置方式有两种: 第一种,采用方式如下: [UINavigationBar appearance] 这种是对一类对象的默 ...

随机推荐

  1. 编程题:1. var person = '{name:"Lily",sex:"famale",age:24,country:"US"}';将person转换成JSON对象并便利每个属性值。

    /// <summary> /// Json工具类 /// </summary> public class JsonUtility { private static JsonU ...

  2. JS Debug

    任何一个编程者都少不了要去调试代码,不管你是高手还是菜鸟,调试程序都是一项必不可少的工作.一般来说调试程序是在编写代码之后或测试期修改Bug 时进行的,往往在调试代码期间更加能够体现出编程者的水平高低 ...

  3. The android gradle plugin version 2.3.0-beta2 is too old, please update to the latest version.

    编译项目的时候,报如下错误: Error:(, ) A problem occurred evaluating project ':app'. > Failed to apply plugin ...

  4. HDU4612 Warm up —— 边双联通分量 + 重边 + 缩点 + 树上最长路

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4612 Warm up Time Limit: 10000/5000 MS (Java/Ot ...

  5. 关于追踪qemu 源码函数路径的一个方法

    这阵子一直在研究qemu 磁盘io路径的源码,发现直接看代码是意见非常低效率的事情,qemu是一个比较庞大的家伙(源码部分大概154MB,完全由C语言来完成),整个结构也都非常地复杂,所以从代码上研究 ...

  6. poj 3321(带时间戳 + 区间统计)

    题目描述: 给定一颗树,求某个节点的子树的val值之和 可以用树的前序遍历给每一个节点编号,从而可以确定一个节点的子树的范围,这样就可以进行直接在区间上进行统计了. vector < int & ...

  7. mysql 数据库的名称不能以数字开头

    mysql  数据库的名称不能以数字开头

  8. Ubuntu 12.04的gnome classic panel添加程序快捷键(转载)

    How to add applets to the Gnome classic panel in Ubuntu 12.04 转自:http://www.borfast.com/blog/how-add ...

  9. bzoj 3714: [PA2014]Kuglarz【最小生成树】

    参考:https://blog.csdn.net/aarongzk/article/details/48883741 没想到吧.jpg 来自题解: "如果用sum[i]表示前i个杯子底球的总 ...

  10. 洛谷 P2774 方格取数问题【最小割】

    因为都是正整数,所以当然取得越多越好.先把所有点权加起来,黑白染色后,s向所有黑点连流量为点权的边,所有白点向t连流量为点权的边,然后黑点向相邻的四个白点连流量为inf的边,表示不可割,这样一来,对于 ...