iOS 用 CollectionView 在滚动后出现“残影”

2015-05-08 15:14:45 +08:00
 DavidHu

用CollectionView画了一个课程表界面,但是在上下滑动以后前面的第几节课的标号会出现“残影”。
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
UILabel *classNum = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 25, CellHieght)];
if (indexPath.row == 0) {
cell.backgroundColor = [UIColor whiteColor];
classNum.text = [NSString stringWithFormat:@"%ld", indexPath.section + 1];
classNum.font = [UIFont systemFontOfSize:12];
classNum.textAlignment = NSTextAlignmentCenter;
[cell addSubview:classNum];
}
else{
cell.backgroundColor = [UIColor lightGrayColor];
}
return cell;
还请各位指教,个人感觉应该是没有reuse的问题
GitHub地址在这: https://github.com/DavidHu0921/ClassBoxForCityDLUT
问题代码在Controller/ClassCalendar/test里面

3700 次点击
所在节点    iDev
14 条回复
DavidHu
2015-05-08 15:18:59 +08:00
唉?Markdown语法为什么不稳定啊,打了tab还是不显示成代码格式……
finian
2015-05-08 15:50:06 +08:00
@DavidHu 用 ``` block
ycge234
2015-05-08 18:03:44 +08:00
1)不使用可复用的cell
2)每次遍历cell删除所有子节点
Elethom
2015-05-08 18:06:24 +08:00
不想解釋,請回去看基礎部分。Reuse 機制你都不清楚。
PhilCai
2015-05-08 18:32:15 +08:00
[cell addSubview:classNum];就是这句话
cheng4741
2015-05-08 18:32:15 +08:00
你这复用的什么鬼,每次复用cell都重新加个lable上来
DavidHu
2015-05-08 20:01:33 +08:00
@PhilCai 所以应该写在什么地方……
PhilCai
2015-05-09 00:05:08 +08:00
@DavidHu 你看tableview怎么用的,就知道collectionview怎么用了
engin
2015-05-09 16:26:04 +08:00
对你的label做一个标记tag,不用每次都充分添加。

```
NSInteger numberLabelTag = 1024;
UILabel *classNum = (UILabel*)[cell viewWithTag:numberLabelTag];

if (indexPath.row == 0) {

if (nil==classNum) {
classNum = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 25, CellHieght)];
classNum.tag = numberLabelTag;
[cell addSubview:classNum];
}
cell.backgroundColor = [UIColor yourColor];

cell.backgroundColor = [UIColor whiteColor];
classNum.text = [NSString stringWithFormat:@"%ld", indexPath.section + 1];
classNum.font = [UIFont systemFontOfSize:12];
classNum.textAlignment = NSTextAlignmentCenter;

}
else{
classNum.hidden = YES;
cell.backgroundColor = [UIColor lightGrayColor];
}
```
newtonisaac
2015-05-10 01:06:35 +08:00
@engin 兄台iOS哪里学的?
DavidHu
2015-05-10 01:48:16 +08:00
@engin 这样会造成滚动后前面的数字消失。我目前的办法是新建一个cell,然后引入,再添加到collection 里面,在else里面写一句classNum.text = nil,就没有问题了。
感谢回复
engin
2015-05-10 10:02:50 +08:00
@DavidHu 不明白你的意思,你要的是一个悬浮的section head效果吗?
engin
2015-05-10 10:04:33 +08:00
@newtonisaac 《iOS三天速成》 —_—
DavidHu
2015-05-10 13:14:37 +08:00
@engin 不是,不过现在问题暂时解决啦,不过是治标不治本的办法。后续再改吧,多谢~

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/189493

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX