最近在开发过程中,发现的一个问题,当cell重用之后,数据更新,但是里面其中两个是完全一样的数据,所以当button的tag标记写到if判断里面的时候就不会走tag的设置
代码如下:
ssddddddd *cell = [tableView dequeueReusableCellWithIdentifier:reuseID];
if (!cell) {
cell = [[ssddddddd alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseID];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell.button addTarget:self action:@selector(xxxxx:) forControlEvents:UIControlEventTouchUpInside];
cell.button.tag = 1000 + indexPath.section;
}
这种情况就可能出现数据跑偏的问题,后面使用tag的值也会出现变化;
解决方法;
1cell的id的改变,根据section,或者path来标记
2将标记tag写到if外面如下:
ssddddddd *cell = [tableView dequeueReusableCellWithIdentifier:reuseID];
if (!cell) {
cell = [[ssddddddd alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseID];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
[cell.button addTarget:self action:@selector(xxxxx:) forControlEvents:UIControlEventTouchUpInside];
cell.button.tag = 1000 + indexPath.section;
完
本文探讨了UITableView在cell重用时遇到的数据跑偏问题。具体表现为按钮的tag值未能正确设置导致数据更新不准确。文章给出了两种解决方案:一是通过section或indexPath对cell进行唯一标识;二是将tag值的设置移出if判断之外。
1万+

被折叠的 条评论
为什么被折叠?



