UITableView简介
UITableView是我们ios开发中最常用的一个控件,学会UItableView的使用对我们的开发工作也是相当重要。
基本用法
使用Xcode新建一个空项目,在默认的UIViewController中显示我们的UITableView,首先在viewDidLoad方法中初始化tableView
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:tableView];
我在开发中的习惯是新创建出来的view立即添加到父中去以免后面会忘掉,除非有一些其他的逻辑要处理。一个UITableView要能显示一般需要控制器实现两个代理方法UITableViewDataSource, UITableViewDelegate前一个代理方法是为tableView提供数据,后一个主要是处理和tableView属性相关的比如:行高、cell的点击等。
分别查看这两个代理方法你会发现UITableViewDataSource的代理方法有两个是必须实现的,而UITableViewDelegate所有的方法都是可选的,那么创建一个tableView的最简单的方式就是实现DataSource中的两个方法就可以了。
@required
// 返回一个UITableView的总行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
// 返回每一个cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
需要说明的是这里的tableView是单组的,废话不多说创建一个简单tableView的代码如下:
//
// ViewController.m
// UITableViewDemo
//
// Created by code_xq on 16/2/28.
// Copyright © 2016年 code_xq. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()<UITableViewDataSource>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:tableView];
tableView.dataSource = self;
}
#pragma mark - 代理方法<UITableViewDataSource>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// 创建cell
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
cell.textLabel.text = [NSString stringWithFormat:@"cell--%ld", indexPath.row];
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
需要说明的cell的创建方法init方法一共有两个参数,第一个参数是一个枚举苹果官方为我们提供了几种cell的显示模式,第二个参数是一类cell的统一标示,cell重用时会用到。
tableView中cell的重用
考虑到数据量大的情况,不可能有一万条数据就按照上面的方式创建一万个cell这样会非常吃内存,效率也会大大降低,于是苹果为我们实现了一套cell重用机制,一次性只创建有限个cell显示在屏幕上,滑动时要用到的话再去创建,把离开屏幕中看不见cell放到一个缓存集合中去,当下次要创建时先从集合中取,如果有就不用创建了,这样会大大提高tableView的性能。
- 传统做法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// 定义static变量,由于本方法会被调用多次如果用局部变量的话有点浪费
static NSString *const ID = @"cell";
// 先从缓存中找,如果没有再创建
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
cell.textLabel.text = [NSString stringWithFormat:@"cell--%ld", indexPath.row];
return cell;
}
因为这段代码经常会用到,我习惯把他放到我的Xcode的代码仓库中这是开发中的一个小技巧。
- 新做法
不用写if判断,可以直接在ViewDidLoad方法中对cell进行注册,最后直接从缓存中取
#import "ViewController.h"
// 定义static变量
static NSString *const ID = @"cell";
@interface ViewController ()<UITableViewDataSource>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:tableView];
tableView.dataSource = self;
// cell注册
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
}
#pragma mark - 代理方法<UITableViewDataSource>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// // 定义static变量,由于本方法会被调用多次如果用局部变量的话有点浪费
// static NSString *const ID = @"cell";
// 先从缓存中找,如果没有再创建
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// if (!cell) {
// cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
// }
cell.textLabel.text = [NSString stringWithFormat:@"cell--%ld", indexPath.row];
return cell;
}