解决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. tar 命令压缩时报错 tar: Removing leading `/' from member names

    在使用tar命令进行压缩打包的时候我们常常会遇到下面的错误.虽然它不会影响我们最后的压缩打包,但是间接说明了我们的命令是有问题的.接下来我们来看看解决的方法. 报错内容: [root@haha ~]# ...

  2. python总结--目录(转)

    python模块   [Python]随机数与随机字符串  举例说明Python的CSV模块   python模块之smtplib: 用python发送SSL/TLS安全邮件   python模块之e ...

  3. 【angular5项目积累总结】文件下载

    download() { const token = localStorage.getItem('token'); let headers: HttpHeaders = new HttpHeaders ...

  4. 【JavaScript 从零开始】 数字 文本 包装对象

    JavaScript中的算术运算 JavaScript 还自称更加复杂的算术运算,这些复杂的运算通过作为Math对象的属性定义的函数和常量来实现: Math.pow(2,53) //=>9007 ...

  5. Jsp&Servlet入门级项目全程实录第2讲

    惯例广告一发,对于初学真,真的很有用www.java1234.com,去试试吧! 1.导入jquery-easyui-1.3.3包( http://www.jeasyui.com/) 2.在页面导入e ...

  6. Struts2和MVC的简单整合

    1.首先还是创建一个简单Maven的项目,导入jar包, <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:x ...

  7. java 对CSV 文件的读取与生成

    CSV文件是以逗号分隔值的文件格式,一般用WORDPAD或记事本(NOTE),EXCEL打开.CSV(逗号分隔值)是一种用来存储数据的纯文本文件,通常都是用于存放电子表格或数据的一种文件格式,对于CS ...

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

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

  9. 撩课-Web大前端每天5道面试题-Day21

    1.对async.await的理解,内部原理? ①async---声明一个异步函数: 自动将常规函数转换成promise,返回值也是一个promise对象, 只有async函数内部的异步操作执行完,才 ...

  10. Spark of work

    Today I attended a meeting of reviewing code,  and I learned a lot from it. In the discuss, we found ...