近来用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. Dynemic Web Project中使用servlet的 doGet()方法接收来自浏览器客户端发送的add学生信息形成json字符串输出到浏览器并保存到本地磁盘文件

    package com.swift.servlet; import java.io.FileOutputStream;import java.io.IOException;import java.io ...

  2. Java传值分析

    public class Example{String str=new String("good");char[] ch={'a','b','c'};public static v ...

  3. Git笔记(流水账)

    命令git checkout -- readme.txt意思就是,把readme.txt文件在工作区的修改全部撤销,这里有两种情况: 一种是readme.txt自修改后还没有被放到暂存区,现在,撤销修 ...

  4. JZOJ 3463. 【NOIP2013模拟联考5】军训

    3463. [NOIP2013模拟联考5]军训(training) (Standard IO) Time Limits: 2000 ms  Memory Limits: 262144 KB  Deta ...

  5. 关于js中onclick字符串传参问题(html="")

    规则: 外变是“”,里面就是‘’外边是‘’,里边就是“”   示例: var a="111"; var html="<a onclick='selecthoods( ...

  6. 748. Shortest Completing Word

    https://leetcode.com/problems/shortest-completing-word/description/ class Solution { public: string ...

  7. C++从键盘读入数组并存储

    C++从键盘读取任意长度的数组,现总结如下: //读取指定长度的数组 int main() { int n = 0; cin >> n; vector<int> p(n); f ...

  8. HDU:2846-Repository

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2846 Repository Time Limit: 2000/1000 MS (Java/Others) ...

  9. 洛谷P4231 三步必杀

    题目描述: $N$ 个柱子排成一排,一开始每个柱子损伤度为0. 接下来勇仪会进行$M$ 次攻击,每次攻击可以用4个参数$l$ ,$r$ ,$s$ ,$e$ 来描述: 表示这次攻击作用范围为第$l$ 个 ...

  10. 9、python中的控制流

    学习完python的基础与数据后,我们就可以编写一些简单的命令了.但这时我们发现,目前位置写出来的程序都是自上而下顺序地执行的.要想程序改变这种自上而下的流程多一点变化,我们就要学习三种程序中的语句. ...