iOS 用 CollectionView 在滚动后出现“残影” - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
DavidHu
V2EX    iDev

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

  •  
  •   DavidHu 2015-05-08 15:14:45 +08:00 4304 次点击
    这是一个创建于 3813 天前的主题,其中的信息可能已经有所发展或是发生改变。

    用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里面

    14 条回复    2015-05-10 13:14:37 +08:00
    DavidHu
        1
    DavidHu  
    OP
       2015-05-08 15:18:59 +08:00
    唉?Markdown语法为什么不稳定啊,打了tab还是不显示成代码格式……
    finian
        2
    finian  
       2015-05-08 15:50:06 +08:00
    @DavidHu 用 ``` block
    ycge234
        3
    ycge234  
       2015-05-08 18:03:44 +08:00   1
    1)不使用可复用的cell
    2)每次遍历cel删除所有子节点
    Elethom
        4
    Elethom  
       2015-05-08 18:06:24 +08:00   1
    不想解,回去看基部分。Reuse 制你都不清楚。
    PhilCai
        5
    PhilCai  
       2015-05-08 18:32:15 +08:00 via iPhone   1
    [cell addSubview:classNum];就是这句话
    cheng4741
        6
    cheng4741  
       2015-05-08 18:32:15 +08:00   1
    你这复用的什么鬼,每次复用cell都重新加个lable上来
    DavidHu
        7
    DavidHu  
    OP
       2015-05-08 20:01:33 +08:00
    @PhilCai 所以应该写在什么地方……
    PhilCai
        8
    PhilCai  
       2015-05-09 00:05:08 +08:00 via iPhone
    @DavidHu 你看tableview怎么用的,就知道collectionview怎么用了
    engin
        9
    engin  
       2015-05-09 16:26:04 +08:00   1
    对你的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
        10
    newtonisaac  
       2015-05-10 01:06:35 +08:00 via iPad
    @engin 兄台iOS哪里学的?
    DavidHu
        11
    DavidHu  
    OP
       2015-05-10 01:48:16 +08:00
    @engin 这样会造成滚动后前面的数字消失。我目前的办法是新建一个cell,然后引入,再添加到collection 里面,在else里面写一句classNum.text = nil,就没有问题了。
    感谢回复
    engin
        12
    engin  
       2015-05-10 10:02:50 +08:00
    @DavidHu 不明白你的意思,你要的是一个悬浮的section head效果吗?
    engin
        13
    engin  
       2015-05-10 10:04:33 +08:00
    @newtonisaac 《iOS三天速成》 _
    DavidHu
        14
    DavidHu  
    OP
       2015-05-10 13:14:37 +08:00
    @engin 不是,不过现在问题暂时解决啦,不过是治标不治本的办法。后续再改吧,多谢~
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1166 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 25ms UTC 17:40 PVG 01:40 LAX 10:40 JFK 13:40
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86