近来用Tableview做了一个九宫格。过程中碰到了两个cell复用问题。

问题一:

在cell中为button添加addTarget点击事件时,出现后面的cell会重叠它前面cell的事件。代码如下:

static NSString *CellWithIdentifier = @"DiscoverHomeTableViewCell";
DiscoverHomeTableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellWithIdentifier forIndexPath:indexPath];
cell1.delegate = self;
[cell1.btnMore addTarget:self action:@selector(btnMoreDisplay) forControlEvents:UIControlEventTouchUpInside];
cell1.labTitle.text = @"热门";
cell1.listExhibit = _homeVo.listExhibit;
cell1.dType = D_TYPE_1;
cell1.navigationController = self.navigationController;
[cell1.tableView reloadData];
return cell1;

经过调试确实是复用了之前cell的事件。在此用协议代理可解决这一问题,用协议来进行处理点击事件。

#pragma mark DiscoverHomeTableViewCellDelegate
- (void)ActionWithTap:(NSString *)type withData:(id)data{
if ([type isEqualToString:D_TYPE_1]) {
[self btnMoreDisplay];
}
}

问题二:

在UITableViewCell中,进行手写代码方式添加控件,这时在cell复用时,会出现重叠控件及控件中的内容。因为每一个cell都是重新添加的,前面的cell会覆盖在后面的cell上。于是强制对cell中添加的控件进行了清空,cell复用不变,只是新的cell加载前,都会对上一个cell的内容进行清空。代码如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifer = @"DiscoverHomeInnerTableViewCell";
DiscoverHomeInnerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifer forIndexPath:indexPath];
//TODO 解决cell复用重叠问题
for (UIView *subview in [cell.contentView subviews]) {
[subview removeFromSuperview];
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//button相关设置
[cell.contentView addSubview:button];
UILabel *lab = [[UILabel alloc] init];
//lab相关设置
[cell.contentView addSubview:lab];

有关UITableView--cell复用问题的更多相关文章

  1. UITableView cell复用出错问题 页面滑动卡顿问题 & 各杂七杂八问题

    UITableView 的cell 复用机制节省了内存,但是有时对于多变的自定义cell,重用时会出现界面出错(例如复用出错,出现cell混乱重影).滑动卡顿等问题,这里只简单敲下几点复用出错时的解决 ...

  2. collectionView 防止cell复用的方法

    collectionView 防止cell复用的方法 一: //在创建collectionView的时候注册cell(一个分区) UICollectionViewCell *cell=[collect ...

  3. 解决UICollectionView的Cell复用引起的布局混乱问题

    解决UICollectionView的Cell复用引起的布局混乱问题   问题复现.gif 查了一下度娘发现没有好的解决办法,于是发动自己的聪明才智,终于找到如下解决办法(充分证明了自己动手丰衣足食啊

  4. UITableView中复用cell显示信息错乱

    UITableView继承自UIScrollview,是苹果为我们封装好的一个基于scroll的控件.上面主要是一个个的 UITableViewCell,可以让UITableViewCell响应一些点 ...

  5. IOS第八天(7:UITableViewController新浪微博,cell 复用的简单写法优化和cell高度从模型中获取)

    *********** #import "HMViewController.h" #import "HMStatus.h" #import "HMSt ...

  6. UITableview cell 的多选

    利用NSMutableDictionary  key值 来改变cell的状态 -(void)createUI{ table = [[UITableView alloc]initWithFrame:CG ...

  7. Why does uitableview cell remain highlighted?

    What would cause a tableview cell to remain highlighted after being touched? I click the cell and ca ...

  8. UITableView cell中label自动换行和自定义label自动换行

    换行的前提必须是有足够的高度 才能换 否则不显示超出部分 所以,在设置label换行的时候 要考虑cell的高度,cell的高度也要变化,废话不多说,来段代码: cell.label.text=[di ...

  9. UITableView Cell 弹簧动画效果

    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath ...

  10. 【iOS开发-68】APP下载案例:利用tableView自带的cell布局+缓存池cell复用时注意button状态的检查

    (1)效果 (2)源码与资源下载 http://pan.baidu.com/s/1pJLo2PP (3)总结 --核心是利用UITableView里面自带的cell来制作样式同样的cell. 与之对应 ...

随机推荐

  1. 【数学 随机 技巧】cf364D. Ghd

    随机化选讲的例题 John Doe offered his sister Jane Doe find the gcd of some set of numbers a. Gcd is a positi ...

  2. AIDE

    安装 yum install aide 修改配置文件 vim /etc/aide.conf (指定对哪些文件进行检测) /test/chameleon R /bin/ps R+a /usr/bin/c ...

  3. 通过sudo提权方式控制公司人员权限

    #通过visudo编辑/etc/sudoers Runas_Alias OP = root #定义使用sudo的时候以哪个用户执行命令,一般都是使用root #命令别名 Cmnd_Alias NETW ...

  4. 课时5.什么是URL(理解)

    其实浏览器的地址栏中输入的地址就是一个URL. http://127.0.0.1/index.html(浏览器会自动添加:80) http://127.0.0.1:80/index.html(这是浏览 ...

  5. Farm Tour POJ - 2135 (最小费用流)

    When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 <= ...

  6. android shape.xml 文件使用

    设置背景色可以通过在res/drawable里定义一个xml,如下: <?xml version="1.0" encoding="utf-8"?> ...

  7. 面试常问 Java基础 冒泡排序

    冒泡排序就是对一个数组进行排序. 用双层for循环就可以解决. 第一层,决定排序的次数. n个数,进行n-1次排序就行了. 第二层,把相邻的两个数比较,谁小,放到后面和下一个数字比较,谁小谁放到后面, ...

  8. play后面加the不加the如何分辨

    play表示“参加(某种球类运动或棋牌类的活动)”时,不需要定冠词the,后面直接加球类运动名称或棋牌类活动名称,可根据实际情况翻译成“打,踢,下”等.例如: 1) He often plays fo ...

  9. Hyper-V 虚拟机快照:常见问题

    发布时间: 2009年3月 更新时间: 2010年12月 应用到: Windows Server 2008 什么是虚拟机快照? 虚拟机快照可捕获正在运行的虚拟机的状态.数据和硬件配置. 快照有哪些用途 ...

  10. 【Word Ladder II】cpp

    题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...