解决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. mysql预编译

    一.背景: 用Mybatis+mysql的架构做开发,大家都知道,Mybatis内置参数,形如#{xxx}的,均采用了sql预编译的形式,举例如下: <select id=”aaa” param ...

  2. java-forkjoin框架的使用

    ForkJoin是Java7提供的原生多线程并行处理框架,其基本思想是将大任务分割成小任务,最后将小任务聚合起来得到结果.fork是分解的意思, join是收集的意思. 它非常类似于HADOOP提供的 ...

  3. oracle 一对多数据分页查询筛选

    今天项目测试运行的时候,遇到了一个奇怪的问题,这个问题说起来按sql语法的话是没有错误的 但是呢按照我们的业务来做区分就有些逻辑上的错误了, 下面请听我慢慢道来,在数据库中有两个数据, 先来看下第一次 ...

  4. unity编辑器之自动提示订外卖

    1.问题来源        事情一忙,忘记叫外卖是常有的事,到了12点同事们都吃上了饭,你却只能挨饿,估计很多程序员都有这种经历吧,这里我们来做一个unity编辑器准点提示订外卖服务的功能.   2. ...

  5. php的ajax简单实例

    很早就听闻ajax的名声,但是却一直不知道怎么用,今天自己捣鼓了一下,竟然会用了,哈哈哈哈. 为了防止我自己忘记,现在把这个简单的实例记录下.这个实例是网上搜的,文末附上链接. 首先你得有自己的服务器 ...

  6. 快排,归并和Shell排序

    快速排序 快速排序的执行流程: (1) 先从数列中取出一个数作为基准数. (2) 将比这个数大的数全放到它的右边,小于或等于它的数全放到它的左边. (3)再对左右区间重复第二步,直到各区间只有一个数. ...

  7. python实现float/double的0x转化

    1. 问题引出 最近遇到了一个小问题,即: 读取文本文件的内容,然后将文件中出现的数字(包括double, int, float等)转化为16进制0x存储 原本以为非常简单的内容,然后就着手去写了py ...

  8. 微信小程序、微信公众号、H5之间相互跳转

    转自慕课网 一.小程序和公众号 答案是:可以相互关联. 在微信公众号里可以添加小程序. 图片有点小,我把文字打出来吧: 可关联已有的小程序或快速创建小程序.已关联的小程序可被使用在自定义菜单和模版消息 ...

  9. portable-net45+win8

    <PropertyGroup> <TargetFramework>netcoreapp1.1</TargetFramework> <RuntimeFramew ...

  10. SpringMVC配置式开发

    所谓配置式开发是指“处理器类是程序员手工定义,实现了特定接口的类,然后再在SpringMVC 配置文件中对该类进行显示的.明确的注册”的开发方式. 一.处理器映射器HandlerMapping Han ...