UITableView是咱们ios开发中最经常使用的一个控件,学会UItableView的使用对咱们的开发工做也是至关重要。ios
使用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中的两个方法就能够了。ui
@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重用时会用到。code
考虑到数据量大的状况,不可能有一万条数据就按照上面的方式建立一万个cell这样会很是吃内存,效率也会大大下降,因而苹果为咱们实现了一套cell重用机制,一次性只建立有限个cell显示在屏幕上,滑动时要用到的话再去建立,把离开屏幕中看不见cell放到一个缓存集合中去,当下次要建立时先从集合中取,若是有就不用建立了,这样会大大提升tableView的性能。orm
- (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; }