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 · 3665 次点击
    这是一个创建于 3247 天前的主题,其中的信息可能已经有所发展或是发生改变。

    用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)每次遍历cell删除所有子节点
    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   ·   我们的愿景   ·   实用小工具   ·   1190 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 18:15 · PVG 02:15 · LAX 11:15 · JFK 14:15
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.