11-UITableView
UITableView
掌握
- 设置UITableView的dataSource、delegate
- UITableView多组数据和单组数据的展示
- UITableViewCell的常见属性
- UITableView的性能优化(cell的循环利用)
- 自定义Cell
作业
- 使用素材car_simple.plist来展示汽车品牌数据
什么是UITableView
- 在众多移动应用中,能看到各式各样的表格数据

在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView
UITableView继承自UIScrollView,因此支持垂直滚动,而且性能极佳
UITableView的两种样式

如何展示数据
- UITableView需要一个数据源(dataSource)来显示数据
- UITableView会向数据源查询一共有多少行数据以及每一行显示什么数据等
- 没有设置数据源的UITableView只是个空壳
- 凡是遵守UITableViewDataSource协议的OC对象,都可以是UITableView的数据源
tableView和数据源

tableView展示数据的过程
- 调用数据源的下面方法得知一共有多少组数据
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
- 调用数据源的下面方法得知每一组有多少行数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- 调用数据源的下面方法得知每一行显示什么内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
字典转模型

初始MVC
- MVC是一种设计思想,贯穿于整个iOS开发中,需要积累一定的项目经验,才能深刻体会其中的含义和好处
- MVC中的三个角色
- M:Model,模型数据
- V:View,视图(界面)
- C:Control,控制中心
- MVC的几个明显的特征和体现:
- View上面显示什么东西,取决于Model
- 只要Model数据改了,View的显示状态会跟着更改
- Control负责初始化Model,并将Model传递给View去解析展示
Cell简介
- UITableView的每一行都是一个UITableViewCell,通过dataSource的tableView:cellForRowAtIndexPath:方法来初始化每一行
- UITableViewCell内部有个默认的子视图:contentView,contentView是UITableViewCell所显示内容的父视图,可显示一些辅助指示视图
- 辅助指示视图的作用是显示一个表示动作的图标,可以通过设置UITableViewCell的accessoryType来显示,默认是UITableViewCellAccessoryNone(不显示辅助指示视图),其他值如下:
UITableViewCellAccessoryDisclosureIndicator

UITableViewCellAccessoryDetailDisclosureButton

UITableViewCellAccessoryCheckmark

- 还可以通过cell的accessoryView属性来自定义辅助指示视图(比如往右边放一个开关)
UITableViewCell的contentView
- contentView下默认有3个子视图
- 其中2个是UILabel(通过UITableViewCell的textLabel和detailTextLabel属性访问)
- 第3个是UIImageView(通过UITableViewCell的imageView属性访问)
- UITableViewCell还有一个UITableViewCellStyle属性,用于决定使用contentView的哪些子视图,以及这些子视图在contentView中的位置
UITableViewCellStyleDefault

- UITableViewCellStyleSubtitle

- UITableViewCellStyleValue1

- UITableViewCellStyleValue2

UITableViewCell结构

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
{
// 1.定义一个cell的标识
static NSString *ID = @"mjcell";
// 2.从缓存池中取出cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 3.如果缓存池中没有cell
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
// 4.设置cell的属性...
return cell;
}
11-UITableView的更多相关文章
- CS193p Lecture 11 - UITableView, iPad
UITableView 的 dataSource 和 delegate dataSource 是一种协议,由 UITableView 实现,将 Model 的数据给到 UITableView: del ...
- 升级到iOS9之后的相关适配
iOS9AdaptationTips(iOS9开发学习交流群:458884057) iOS9适配系列教程[中文在页面下方]转自@iOS程序犭袁 (截至2015年9月26日共有10篇,后续还将持续更新. ...
- UITableView-FDTemplateLayoutCell 学习笔记
本文转载至 http://www.tuicool.com/articles/I7ji2uM 原文 http://everettjf.github.io/2016/03/24/learn-uitabl ...
- 地区sql
/*Navicat MySQL Data Transfer Source Server : localhostSource Server Version : 50136Source Host : lo ...
- IOS 11 下适配UITableView
9月份苹果发布了IOS11和Iphone X,这一操作系统一硬件对于开发者适配上面还是造作了不少蛋疼的地方.先来看看IOS 11,这些蛋疼的需要适配的地方: 1.UIScrollView及其子类在IO ...
- [Xcode 实际操作]五、使用表格-(11)调整UITableView的单元格顺序
目录:[Swift]Xcode实际操作 本文将演示如何调整单元格在表格中的位置. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit //首先 ...
- 使用Autolayout实现UITableView的Cell动态布局和高度动态改变
本文翻译自:stackoverflow 有人在stackoverflow上问了一个问题: 1 如何在UITableViewCell中使用Autolayout来实现Cell的内容和子视图自动计算行高,并 ...
- 第八章 交互技术,8.4 Weex 双11会场大规模应用的秒开实战和稳定性保障(作者:鬼道)
8.4 Weex 双11会场大规模应用的秒开实战和稳定性保障 前言 Native 开发的诸多亮点中,流畅体验和系统调用是最多被提及的.流畅体验体现在页面滚动/动画的流畅性,背后是更好的内存管理和更接近 ...
- UITableview中怎么找到每个cell
一个朋友问我:我在每个cell中都添加了两个按钮(记为btnA和btnB),点击btnA时,对应的cell中添加一个子控件,再点击btnB时,对应的cell中的子控件就移除,怎么做到? 百度了一下,发 ...
- UITableView的使用
参考:IOS7.0 programming cookbook. http://www.cnblogs.com/kenshincui/p/3931948.html http://blog.csdn.ne ...
随机推荐
- Choose and divide(唯一分解定理)
首先说一下什么是唯一分解定理 唯一分解定理:任何一个大于1的自然数N,如果N不是质数,那么N可以分解成有限个素数的乘积:例:N=(p1^a1)*(p2^a2)*(p3^a3)......其中p1< ...
- mac U盘安装盘制作
以下面的版本为例: Install_macOS_Sierra_10.12.6.dmg 解压到桌面,然后打命令 sudo /Users/love/Desktop/Install\ macOS\ Sier ...
- Unity Scene Screen.resolutions 分辨率列表
Screen.resolutions 分辨率列表(安卓平台试了不能用此方法,最好用宏定义判断一下平台) C# => public static Resolution[] resolutions; ...
- HDU 2389 ——Rain on your Parade——————【Hopcroft-Karp求最大匹配、sqrt(n)*e复杂度】
Rain on your Parade Time Limit:3000MS Memory Limit:165535KB 64bit IO Format:%I64d & %I64 ...
- Net-SNMP(V3协议)安装配置笔记(CentOS 5.2)(转)
原出处:http://blog.ihipop.info/2010/03/722.html 为了这颗仙人掌(cacti),我必须先部署(Net-SNMP), 同时我为了安全因素,也为了简便考虑,决定采用 ...
- aliyun maven repository
<mirrors> <mirror> <id>alimaven</id> <name>aliyun maven</name> & ...
- c语言 Implement strStr()【Leetcode】
实现在一个母字符串中找到第一个子字符串的位置. #include <stdio.h> #include <string.h> #define _IRON_TRUE 1 #def ...
- 跨平台移动开发phonegap/cordova 3.3全系列教程-helloworld
1. 建立专案(cordova) 打开cmd命令行 cordova create ACESMobile aces.mobile ACES cd aces mobile 如图 2. 安装插件 ...
- java:图片压缩
java使用google开源工具实现图片压缩 :http://www.cnblogs.com/linkstar/p/7412012.html
- springboot文件上传: 单个文件上传 和 多个文件上传
单个文件上传 //文件上传统一处理 @RequestMapping(value = "/upload",method=RequestMethod.POST) @ResponseBo ...