iOS中UITableViewCell的重用问题解决方案
来源:互联网 发布:let it out frances 编辑:程序博客网 时间:2024/04/29 06:45
UITableView中的cell可以有很多,一般会通过重用cell来达到节省内存的目的:通过为每个cell指定一个重用标识符(reuseIdentifier),即指定了单元格的种类,当cell滚出屏幕时,会将滚出屏幕的单元格放入重用的queue中,当某个未在屏幕上的单元格要显示的时候,就从这个queue中取出单元格进行重用。
但对于多变的自定义cell,有时这种重用机制会 重影出错。
解决方法:
方法1 将获得cell的方法从- (UITableViewCell*)dequeueReusableCellWithIdentifier:(NSString*)identifier 换为-(UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
重用机制调用的就是dequeueReusableCellWithIdentifier这个方法,方法的意思就是“出列可重用的cell”,因而只要将它换为cellForRowAtIndexPath(只从要更新的cell的那一行取出cell),就可以不使用重用机制,因而问题就可以得到解决,虽然可能会浪费一些空间。
方法一:
示例代码:
[plain]
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //改为以下的方法
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; //根据indexPath准确地取出一行,而不是从cell重用队列中取出
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//...其他代码
}
方法二:
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //出列可重用的cell
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
else
{
//删除cell的所有子视图
{
NSArray *subViews = cell.contentView.subviews;
for (UIView *view in subViews) {
[view removeFromSuperview];
}
}
// while ([cell.contentView.subviews lastObject] != nil)
// {
// [(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview];
// }
// }
//...其他代码
}
方法三:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d%d", [indexPath section], [indexPath row]];//以indexPath来唯一确定cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //出列可重用的cell
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//...其他代码
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d%d", [indexPath section], [indexPath row]];//以indexPath来唯一确定cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //出列可重用的cell
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//...其他代码
}
- iOS中UITableViewCell的重用问题解决方案
- iOS UITableViewCell的重用机制出现问题解决
- iOS-UITableviewCell的重用机制
- iOS-UITableviewCell的重用机制
- iOS-UITableviewCell的重用机制
- iOS-UITableviewCell的重用机制
- iOS-UITableviewCell的重用机制
- iOS-UITableviewCell的重用机制
- iOS-UITableviewCell的重用机制
- iOS UITableviewCell的重用机制
- iOS-UITableviewCell的重用机制
- iOS-UITableviewCell的重用机制
- iOS-UITableviewCell的重用机制
- IOS学习-UITableViewCell的重用机制原理
- [IOS]轻松理解UITableViewCell的重用机制
- iOS之UITableViewCell的重用机制
- iOS 【UIKit-UITableViewCell的重用方式&static】
- iOS关于UITableViewCell的重用机制
- JS跳转页面的几种方式
- 解释器模式(Interpreter)Java
- VMware vCenter Server Appliance 部署向导文档
- 堡垒机-麒麟开源堡垒机 V 1.3 正式发布
- easyui tree 多级节点的的某个节点的所有子节点id查询
- iOS中UITableViewCell的重用问题解决方案
- Oracle 使用Job详解
- ACM 数据结构 线段树 HDU 1698Just a Hook
- ScrollView不能滚动问题
- MyBatis缓存
- 基于汉字字频特征实现99.99%准确率的新闻文本分类器(五)
- 第11周项目5 - 摩托车继承自行车和机动车
- Tomcat容器等级
- github上的安卓开源库汇总