网站建设专有名词,免费申请自己的网站,上海建设安全生产协会网站,自己建网站可以赚钱吗UITableView
列表的特点#xff1a;
数据量大样式较为统一通常需要分组垂直滚动通常可视区只有一个 - 视图的复用 UITableViewDataSource
UITableView作为视图#xff0c;只负责展示#xff0c;协助管理#xff0c;不管理数据
需要开发者为UITableView提供展示所需…UITableView
列表的特点
数据量大样式较为统一通常需要分组垂直滚动通常可视区只有一个 - 视图的复用 UITableViewDataSource
UITableView作为视图只负责展示协助管理不管理数据
需要开发者为UITableView提供展示所需要的数据及Cell
通过delegate的模式开发者需要实现UITableViewDataSource
require numberOfRowsInSection:(NSInteger)section;cellForRowAtIndexPath:(NSIndexPath *)indexPath;
UITableViewCell默认提供的样式 都是常用的cell布局
UITableViewCell的复用及其原理
//每当滚动tableView有cell要进入可视区时系统都会自动回调此dataSource方法
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {UITableViewCell* cell [tableView dequeueReusableCellWithIdentifier: id];if (!cell) {cell [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: id];count;}cell.textLabel.text [NSString stringWithFormat: 主标题 - %, (indexPath.row)];cell.detailTextLabel.text 副标题;cell.imageView.image [UIImage imageNamed: image.png];return cell;
}- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return 66;
}当你需要66个cell来展示数据时而可视区至多显示13个cell开始cell为空系统会先创建13个cell并设置自定义的id做标记滑动tableView滑出可视区的cell系统会将其放入cell回收池划入可视区的cell是根据id从回收池取出的同类型的cell来进行复用 因此本质上系统只需创建了13个cell这样极大地减少了内存消耗提高了程序性能 以上是cell复用的方法需要对cell进行判空接下来介绍一种不需要判空的方法——cell的注册机制
// 在viewDidLoad初始化方法中进行注册
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:id];// 在dataSource协议方法中获取已注册的单元格cell
UITableViewCell *cell [tableView dequeueReusableCellWithIdentifier:id forIndexPath:indexPath];在使用注册机制后就可以在dataSource方法中通过标识符快速复用已注册的cell而且在cell为空时会自动利用注册cell时提供的类创建一个新的cell并返回而无需手动创建和管理每个cell实例
自定义Cell
【iOS】自定义Cell
demo
UITableViewDemo on GitHub