UITableViewCell重用的问题
UITableView中有两种重用Cell的方法:
- - (id)dequeueReusableCellWithIdentifier:(NSString *)identifier;
- - (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);
在iOS 6中dequeueReusableCellWithIdentifier:被dequeueReusableCellWithIdentifier:forIndexPath:所取代。如此一来,在表格视图中创建并添加UITableViewCell对象会变得更为精简而流畅。而且使用dequeueReusableCellWithIdentifier:forIndexPath:一定会返回cell,系统在默认没有cell可复用的时候会自动创建一个新的cell出来。
使用dequeueReusableCellWithIdentifier:forIndexPath:的话,必须和下面的两个配套方法配合起来使用:
- // Beginning in iOS 6, clients can register a nib or class for each cell.
- // If all reuse identifiers are registered, use the newer -dequeueReusableCellWithIdentifier:forIndexPath: to guarantee that a cell instance is returned.
- // Instances returned from the new dequeue method will also be properly sized when they are returned.
- - (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(5_0);
- - (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);
1、如果是用NIB自定义了一个Cell,那么就调用registerNib:forCellReuseIdentifier:
2、如果是用代码自定义了一个Cell,那么就调用registerClass:forCellReuseIdentifier:
以上这两个方法可以在创建UITableView的时候进行调用。
这样在tableView:cellForRowAtIndexPath:方法中就可以省掉下面这些代码:
- static NSString *CellIdentifier = @"Cell";
- if (cell == nil)
- cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
取而代之的是下面这句代码:
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
一、使用NIB
1、xib中指定cell的Class为自定义cell的类型(不是设置File's Owner的Class)
2、调用registerNib:forCellReuseIdentifier:向数据源注册cell
- [_tableView registerNib:[UINib nibWithNibName:@"CustomCell" bundle:nil] forCellReuseIdentifier:kCellIdentify];
3、在tableView:cellForRowAtIndexPath:中使用dequeueReusableCellWithIdentifier:forIndexPath:获取重用的cell,如果没有重用的cell,将自动使用提供的nib文件创建cell并返回(如果使用dequeueReusableCellWithIdentifier:需要判断返回的是否为空)
- CustomCell *cell = [_tableView dequeueReusableCellWithIdentifier:kCellIdentify forIndexPath:indexPath];
4、获取cell时如果没有可重用cell,将创建新的cell并调用其中的awakeFromNib方法
二、不使用NIB
1、重写自定义cell的initWithStyle:withReuseableCellIdentifier:方法进行布局
2、注册cell
- [_tableView registerClass:[CustomCell class] forCellReuseIdentifier:kCellIdentify];
3、在tableView:cellForRowAtIndexPath:中使用dequeueReusableCellWithIdentifier:forIndexPath:获取重用的cell,如果没有重用的cell,将自动使用提供的class类创建cell并返回
- CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentify forIndexPath:indexPath];
4、获取cell时如果没有可重用的cell,将调用cell中的initWithStyle:withReuseableCellIdentifier:方法创建新的cell
UITableViewCell重用的问题的更多相关文章
- 你真的了解UITableViewCell重用吗?
一:首先查看一下关于UITableViewCell重用的定义 - (nullable __kindof UITableViewCell *)dequeueReusableCellWithIdentif ...
- ios UITableViewCell重用问题
在写sina 微博界面的过程中使用到了cell,那么就是在cell上添加一些控件,但是由于每条微博的内容都是不同的,所以在显示的过程中,出现了内容重叠的问题,其实就是UITableViewCell重用 ...
- 捉襟见肘之UITableViewCell重用引发的问题
我记录一下自己如何解决cell内容重叠的问题 首先,复习一下:http://blog.csdn.net/omegayy/article/details/7356823 UITableViewCell的 ...
- UITableViewCell重用机制
UITableView是iOS开发中使用频率非常高的一个控件,它常被用来展示信息列表,尽管信息数据可能非常多,但UITableView消耗的资源却并不会随着展示信息的增多而变大,这都要得益于UITab ...
- 禁用UITableViewCell 重用机制
有时候不想让Cell重用,怎么办勒.接下来介绍两种方法 方法一 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAt ...
- UITableviewCell 重用内存
转载自:http://www.cnblogs.com/tangbinblog/p/3371545.html 重用实现分析 查看UITableView头文件,会找到NSMutableArray* vi ...
- 有关UITableviewCell 重用内存 内部解析
重用实现分析 查看UITableView头文件,会找到NSMutableArray* visiableCells,和NSMutableDictnery* reusableTableCells两个结构 ...
- UITableViewCell重用导致内容混乱方案
UITableViewCell *cell=nil; static NSString *reuse=@"cell"; if (cell==nil) { cell=[[UITable ...
- iOS解决cell重用问题
在写sina 微博界面的过程中使用到了cell,那么就是在cell上添加一些控件,但是由于每条微博的内容都是不同的,所以在显示的过程中,出现了内容重叠的问题,其实就是UITableViewCell重用 ...
随机推荐
- (medium)LeetCode 278.First Bad Version
You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
- .NET使用一般处理程序生成验证码
运行的效果图: HTML的代码: <head> <script type="text/javascript"> function changeCode() ...
- [PAT]求集合数据的均方差(15)
#include "stdio.h" #include "malloc.h" #include "math.h" int *getinput ...
- Jmeter组件1. CSV Data Set Config
位置:Test Plan | Add | Config Element | CSV Data Set Config 意义: 脚本参数化 节省CPU跟内存(可以准备好数据文件去代替动态生成数据,节约CP ...
- MVC 项目中为什么会有两个web.config
我们对MVC 并不陌生, 在创建MVC项目时,总会发现,在工程目录 中有两个 web.config 文件,一个是在工程的根目录下,一是在 views 下,两个web.config 中的内容也不尽相同, ...
- 如何对具有端点加密功能的LINE进行取证
LINE又有新动作了,这回默认即启用了端点加密功能,强调确保传输过程的安全,且让我们来看看如何对付新版的LINE. 有启用Letter Sealing就会在昵称前多个锁头的图像. 这手机据犯嫌供称,落 ...
- Shell脚本IF条件判断和判断条件总结
转自:http://m.jb51.net/article/56553.htm 这篇文章主要介绍了Shell脚本IF条件判断和判断条件总结,本文先是给出了IF条件判断的语法,然后给出了常用的判断条件总结 ...
- angular 的ng-view,ngrouter
通过ng-view和ngRouter控制页面显示内容: html: <body ng-app="AngularStore"> <div class="c ...
- NHibernate系列文章三:简单的增删改查询
摘要 上一篇文章只完成了简单的NHibernate安装.配置和连接数据库.这篇文章介绍怎样实现最简单的数据库读写操作. 1. 重构ISessionFactory生成过程 将生成ISessionFact ...
- Android IOS WebRTC 音视频开发总结(六七)-- 在线教育虽火要做好其实不容易
本文主要介绍在线教育这个行业,文章最早发表在我们的微信公众号上,支持原创,详见这里, 欢迎关注微信公众号blackerteam,更多详见www.rtc.help 最近很多朋友在咨询在线教育的事(其实之 ...