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
strom001
V2EX  ›  iDev

为何 cell 无法高亮和选中?

  •  
  •   strom001 · 2015-09-11 16:52:16 +08:00 · 3489 次点击
    这是一个创建于 3151 天前的主题,其中的信息可能已经有所发展或是发生改变。
    - (BOOL )collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
        return YES;
    }
    
    - (void )collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
        UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
        cell.selectedBackgroundView.backgroundColor = [UIColor purpleColor];
    }
    

    这样点选以后颜色没有变化

    5 条回复    2015-09-12 16:18:59 +08:00
    PopeyeLau
        1
    PopeyeLau  
       2015-09-11 17:08:03 +08:00
    应该是在 cellForItemAtIndexPath 方法 设置 cell 的 selectedBackgroundView 跟 backgroundColor 吧.
    如果前面没设置, didSelectItemAtIndexPath 里面取 selectedBackgroundView 应该是 nil.
    l12ab
        2
    l12ab  
       2015-09-11 22:13:26 +08:00
    我怎么记得默认带选中效果?
    kobe1941
        3
    kobe1941  
       2015-09-11 22:55:54 +08:00
    一般子类化 cell ,然后自行设置高亮和选中的效果
    strom001
        4
    strom001  
    OP
       2015-09-12 15:23:03 +08:00
    ```objc
    - (void )collectionView:(UICollectionView *)colView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell* cell = [colView cellForItemAtIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor blueColor];
    }

    - (void )collectionView:(UICollectionView *)colView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell* cell = [colView cellForItemAtIndexPath:indexPath];
    cell.contentView.backgroundColor = nil;
    }
    ```
    这段是 xcode 文档里的
    strom001
        5
    strom001  
    OP
       2015-09-12 16:18:59 +08:00
    @PopeyeLau selectedBackgroundView 在 cellForItem 那里设置了也是 nil 啊……另外我在 IB 里设置了 cell 颜色,后来改为默认,结果代码设置颜色就不管用了……
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2710 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 15:27 · PVG 23:27 · LAX 08:27 · JFK 11:27
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.