UITableView介绍 之 基本用法
2016-02-28 21:29
471 查看
UITableView简介
UITableView是我们ios开发中最常用的一个控件,学会UItableView的使用对我们的开发工作也是相当重要。基本用法
使用Xcode新建一个空项目,在默认的UIViewController中显示我们的UITableView,首先在viewDidLoad方法中初始化tableViewUITableView *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; }
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 讲解iOS开发中基本的定位功能实现
- iOS中定位当前位置坐标及转换为火星坐标的方法
- js判断客户端是iOS还是Android等移动终端的方法
- iOS应用中UISearchDisplayController搜索效果的用法
- IOS开发环境windows化攻略
- iOS开发之路--微博“更多”页面
- 浅析iOS应用开发中线程间的通信与线程安全问题
- 检测iOS设备是否越狱的方法
- .net平台推送ios消息的实现方法
- 探讨Android与iOS,我们将何去何从?
- Android、iOS和Windows Phone中的推送技术详解
- iOS推送的那些事
- IOS 改变键盘颜色代码