解决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. 国内maven仓库地址资源汇总

    国内maven仓库地址:阿里云maven仓库,网易163maven仓库,以及其他maven仓库地址. 国内下载maven一般速度都很慢,下载需要很久时间.这里汇总了一些国内的镜像资源 附带pom文件中 ...

  2. 13-hadoop-入门程序

    通过之前的操作, http://www.cnblogs.com/wenbronk/p/6636926.html http://www.cnblogs.com/wenbronk/p/6659481.ht ...

  3. HTML自己整理

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

  4. Week6&7——第一次项目冲刺(Alpha版本)

    Deadline: 2017-11-11 10:00PM,以博客发表日期为准. 评分基准: 按时交 - 有分(需求&原型改进与系统设计-10分,敏捷冲刺-70分),检查的项目包括后文的三个方面 ...

  5. JAVA项目将 Oracle 转 MySQL 数据库转换(Hibernate 持久层)

    项目开发时用的是Oracle数据库,但为了更好的做分布式,做集群,我们要将数据库转成 MySQL! 在数据库迁移中首先要做的事是将 Oracle 的表结构以及数据 克隆到 MySQL 数据库. 这点不 ...

  6. [转]Pass a ViewBag instance to a HiddenFor field in Razor

    本文转自:https://stackoverflow.com/questions/27456983/pass-a-viewbag-instance-to-a-hiddenfor-field-in-ra ...

  7. [转]使用BCP导出导入数据

    本文转自:http://www.cnblogs.com/zerocc/p/3225723.html bcp 实用工具可以在 Microsoft SQL Server 实例和用户指定格式的数据文件间大容 ...

  8. BG.Hadoop.Master

    1. 安装JDK JDK安装包复制到/opt文件夹 cd /opt rpm -ivh jdk-8u121-linux-x64.rpm vim /etc/profile 增加 JAVA_HOME=/us ...

  9. 【转】Java 异步处理简单实践

    同步与异步 通常同步意味着一个任务的某个处理过程会对多个线程在用串行化处理,而异步则意味着某个处理过程可以允许多个线程同时处理. 异步通常代表着更好的性能,因为它很大程度上依赖于缓冲,是典型的使用空间 ...

  10. 【转】详解spring 每个jar的作用

    spring.jar 是包含有完整发布模块的单个jar 包.但是不包括mock.jar, aspects.jar, spring-portlet.jar, and spring-hibernate2. ...