解决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. android学习-Activity启动过程详解

    注:只是说明启动activity的过程(ActivityThread如何与ActivityManagerService简称AmS进行进程间通信调用全过程),不解析android从zygote(受精卵) ...

  2. C#中给滚动条增加鼠标滚动轮控制事件

    当滚动条的父控件获得焦点时,可以使用鼠标的滚动轮来控制滚动条 public partial class Form1 : Form { public Form1() { InitializeCompon ...

  3. PTA (Advanced Level) 1021 Deepest Root

    Deepest Root A graph which is connected and acyclic can be considered a tree. The hight of the tree ...

  4. java面试②基础部分

    2.1.3 讲一下java中int数据占几个字节 java中有几种基本数据类型? 2.1.4. 面向对象的特征有哪些方面 有四大基本特征:封装.抽象.继承.多态 1)封装,即将对象封装成一个高度自治和 ...

  5. 简单Tomcat HTTP RPC框架

    RPC基础知识 什么是RPC? RPC(Remote Procedure Call Protocol)——远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议. ...

  6. 求两个Linux文本文件的交集、差集、并集

    一.交集 sort a.txt b.txt | uniq -d 二.并集 sort a.txt b.txt | uniq 三.差集 a.txt-b.txt: sort a.txt b.txt b.tx ...

  7. SQL SERVER 原来还可以这样玩 FOR XML PATH

    FOR XML PATH 有的人可能知道有的人可能不知道,其实它就是将查询结果集以XML形式展现,有了它我们可以简化我们的查询语句实现一些以前可能需要借助函数活存储过程来完成的工作.那么以一个实例为主 ...

  8. 【拓扑 && 模板】Kosaraju算法

    #include<bits/stdc++.h> using namespace std; ; vector <int> g1[maxn],g2[maxn]; stack < ...

  9. 概述Java集合框架

    JAVA集合框架主要分为三个部分:接口,实现和算法.接口是指以Collection和Map为起始的一系列公用接口,其中还有Vector接口,也就是迭代器,Collection接口下面又有List 和S ...

  10. 各种IDE的使用

    sharpdevelop http://blog.sina.com.cn/s/blog_d1001bff0101di7p.html