解决tableView中cell动态加载控件的重用问题

tableView的cell,有时候需要在运行时取得对应的数据后才能够动态的创建该cell中的控件并加载到该cell中,此时,你一定会遇到重用问题,即使你能做到该cell只根据数值加载了一回控件,你也没法保证不出现重用问题:)

效果(请注意查看,移动下面的格子时,上面出现了重用的问题)

源码:

YXCell.h

//
// YXCell.h
// YXTableView
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import <UIKit/UIKit.h> @interface YXCell : UITableViewCell @property (nonatomic, strong) NSString *count; // 控件个数
@property (nonatomic, assign) BOOL flag; // 控制标签 @end

YXCell.m

//
// YXCell.m
// YXTableView
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "YXCell.h" @implementation YXCell - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{ }
return self;
} @synthesize count = _count;
- (void)setCount:(NSString *)count
{
if ([count intValue] > && _flag == NO)
{
_flag = YES; UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(, , , )];
scrollView.contentSize = CGSizeMake([count intValue]*, ); for (int i = ; i < [count intValue]; i++)
{
UILabel *tmpLabel = [[UILabel alloc] initWithFrame:CGRectMake(i*, , , )];
tmpLabel.text = [NSString stringWithFormat:@"%d", i];
tmpLabel.textAlignment = NSTextAlignmentCenter;
tmpLabel.font = [UIFont fontWithName:@"HelveticaNeue-UltraLight"
size:.f];
[scrollView addSubview:tmpLabel];
} [self addSubview:scrollView];
} _count = count;
} @end

RootViewController.m

//
// RootViewController.m
// YXTableView
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "RootViewController.h"
#import "YXCell.h" #define REUESED_SIZE 100
static NSString *reUsedStr[REUESED_SIZE] = {nil}; // 重用标示
#define REUESED_FLAG reUsedStr[0] @interface RootViewController ()<UITableViewDataSource, UITableViewDelegate> @property (nonatomic, strong) UITableView *mainTableView;
@property (nonatomic, strong) NSArray *dataArray; @end @implementation RootViewController + (void)initialize
{
if (self == [RootViewController class])
{
for (int i = ; i < REUESED_SIZE; i++)
{
reUsedStr[i] = [NSString stringWithFormat:@"YouXianMing_%d", i];
}
}
} - (void)viewDidLoad
{
[super viewDidLoad]; // 数据源
_dataArray = @[[NSString stringWithFormat:@"%d", arc4random()% + ],
[NSString stringWithFormat:@"%d", arc4random()% + ],
[NSString stringWithFormat:@"%d", arc4random()% + ],
[NSString stringWithFormat:@"%d", arc4random()% + ],
[NSString stringWithFormat:@"%d", arc4random()% + ],
[NSString stringWithFormat:@"%d", arc4random()% + ],
[NSString stringWithFormat:@"%d", arc4random()% + ],
[NSString stringWithFormat:@"%d", arc4random()% + ],
[NSString stringWithFormat:@"%d", arc4random()% + ],
[NSString stringWithFormat:@"%d", arc4random()% + ],
[NSString stringWithFormat:@"%d", arc4random()% + ],
[NSString stringWithFormat:@"%d", arc4random()% + ],
[NSString stringWithFormat:@"%d", arc4random()% + ],
[NSString stringWithFormat:@"%d", arc4random()% + ],
[NSString stringWithFormat:@"%d", arc4random()% + ],
[NSString stringWithFormat:@"%d", arc4random()% + ],
[NSString stringWithFormat:@"%d", arc4random()% + ],
[NSString stringWithFormat:@"%d", arc4random()% + ],
[NSString stringWithFormat:@"%d", arc4random()% + ],
[NSString stringWithFormat:@"%d", arc4random()% + ],
[NSString stringWithFormat:@"%d", arc4random()% + ],
[NSString stringWithFormat:@"%d", arc4random()% + ],
[NSString stringWithFormat:@"%d", arc4random()% + ],
[NSString stringWithFormat:@"%d", arc4random()% + ]]; // UITableView
_mainTableView = [[UITableView alloc] initWithFrame:self.view.bounds
style:UITableViewStylePlain];
_mainTableView.delegate = self;
_mainTableView.dataSource = self;
[self.view addSubview:_mainTableView];
} #pragma mark - UITableView delegate dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_dataArray count];
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
YXCell *cell = [tableView dequeueReusableCellWithIdentifier:REUESED_FLAG];
if (cell == nil)
{
cell = [[YXCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:REUESED_FLAG];
} cell.count = _dataArray[indexPath.row]; return cell;
} - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return ;
} @end

几个比较关键的地方:

本例中出现的重用问题由下面部分引发:

如果要解决这个重用问题,我们只能够让这个cell不重用,那就得定义足够多的重用标示才行,改成如下即可:

效果:

总结:

为何要处心积虑弄这种不重用的cell呢?当然,这是为了满足特定的需求而出现的适合于少量的cell的情形,对于这种动态加载的cell,你亲自动手试一下或许就能明白作者本人为何如此设计的用心良苦:)

解决tableView中cell动态加载控件的重用问题的更多相关文章

  1. uGUI动态加载控件位置错误

    最近在使用uGUI时遇到了一个问题,在此记录一下.在Canvas的Render Mode设置为Screen Space-Overlay模式时,动态加载控件是不会发生问题的.但是在Screen Spac ...

  2. uGUI动态加载控件位置错误(转自:https://www.cnblogs.com/mezero/p/4542939.html)

    最近在使用uGUI时遇到了一个问题,在此记录一下.在Canvas的Render Mode设置为Screen Space-Overlay模式时,动态加载控件是不会发生问题的.但是在Screen Spac ...

  3. js动态加载控件jsp页面

    例子1:(具体参照drp中的flow_card_add.jsp)<script>    var rowIndex = 0;     function addOneLineOnClick() ...

  4. Silverlight日记:动态生成DataGrid、行列装换、动态加载控件

    本文主要针对使用DataGrid动态绑定数据对象,并实现行列转换效果. 一,前台绑定 <sdk:DataGrid x:Name="dataGrid2" Style=" ...

  5. 解决MWPhotoBrowser中的SDWebImage加载大图导致的内存警告问题

    下面两种现象,用同一种方法解决 1.解决MWPhotoBrowser中的SDWebImage加载大图导致的内存警告问题 2.突然有一天首页访问图片很慢,至少隔20多秒所有图片才会出来.(解析:app使 ...

  6. 解决hibernate中的懒加载(延迟加载)问题

    解决hibernate中的懒加载(延迟加载)问题   我们在开发的时候经常会遇到延迟加载问题,在实体映射时,多对一和多对多中,多的一样的属性默认是lazy="true"(即,默认是 ...

  7. WinForm的延时加载控件概述

    这篇文章主要介绍了WinForm的延时加载控件,很实用的技巧,在C#程序设计中有着比较广泛的应用,需要的朋友可以参考下   本文主要针对WinForm的延迟加载在常用控件的实现做简单的描述.在进行C# ...

  8. Adroid动态加载Apk-插件化技术框架(动态代理方案)

    技术:Android + java +动态加载+插件化   概述 为什么要使用插件化?在开发中,一个项目只会越做越大.初始版本可能是单一功能,后续可能加上各种风马牛不相及的功能.所以我认为插件化可以使 ...

  9. 发布我的图片预加载控件YPreLoadImg v1.0

    介绍 大家好!很高兴向大家介绍我的图片预加载控件YPreLoadImg.它可以帮助您预加载图片,并且能显示加载的进度,在预加载完成后调用指定的方法. YPreLoadImg控件由一个名为PreLoad ...

随机推荐

  1. docker容器网络通信原理分析(转)

    概述 自从docker容器出现以来,容器的网络通信就一直是大家关注的焦点,也是生产环境的迫切需求.而容器的网络通信又可以分为两大方面:单主机容器上的相互通信和跨主机的容器相互通信.而本文将分别针对这两 ...

  2. python:rs, ws, es = select.select(inputs, [], []) --报错error 10022

    昨晚折腾的1个多钟,直到3点多才睡,感觉自己也是热爱代码了,敲3个多钟一点也不累(其实是为了凌晨6点起来抢票回家了^_^) 练习python中select进行异步通信-防止socket.recv方法阻 ...

  3. Nodejs学习笔记(十六)—Pomelo介绍&入门

    前言&介绍 Pomelo:一个快速.可扩展.Node.js分布式游戏服务器框架 从三四年前接触Node.js开始就接触到了Pomelo,从Pomelo最初的版本到现在,总的来说网易出品还算不错 ...

  4. HTML自己整理

    1.margin:0 auto 表示什么意思?? margin后面如果只有两个参数的话,第一个表示top和bottom,第二个表示left和right因为0 auto,表示上下边界为0,左右则根据宽度 ...

  5. Golang 知识图谱

  6. centos 6.5安装docker

    安装linux,需要系统内核为3.x以上,如果centos版本为7以下,先升级系统内核 1.关闭selinux setenforce 0 sed -i '/^SELINUX=/c\SELINUX=di ...

  7. [磁盘空间]lsof处理文件恢复、句柄以及空间释放问题

    曾经在生产上遇到过一个df 和 du出现的结果不一致的问题,为了排查到底是哪个进程占用了文件句柄,导致空间未释放,首先在linux上面,一切皆文件,这个问题可以使用lsof这个BT的命令来处理(这个哈 ...

  8. 【转】sql server日期比较

    1. 当前系统日期.时间select getdate() 2. dateadd 在向指定日期加上一段时间的基础上,返回新的 datetime 值例如:向日期加上2天select dateadd(day ...

  9. MyEclipse中设置代码块快捷键

    如果想用快捷键生成一段自定义代码,可以通过下面方式设置: Java->Editor->Templates->New 如果要设置或者更改某个快捷键,如要设置保存全部文档的快捷键(系统默 ...

  10. 啰哩吧嗦式讲解在windows 家庭版安装docker

    1.docker是什么,为什么要使用docker Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中, 然后发布到任何流行的 Linux 机器上,也可以实 ...