V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
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 · Sep 11, 2015 · 4288 views
    This topic created in 3884 days ago, the information mentioned may be changed or developed.
    - (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 replies    2015-09-12 16:18:59 +08:00
    PopeyeLau
        1
    PopeyeLau  
       Sep 11, 2015
    应该是在 cellForItemAtIndexPath 方法 设置 cell 的 selectedBackgroundView 跟 backgroundColor 吧.
    如果前面没设置, didSelectItemAtIndexPath 里面取 selectedBackgroundView 应该是 nil.
    l12ab
        2
    l12ab  
       Sep 11, 2015
    我怎么记得默认带选中效果?
    kobe1941
        3
    kobe1941  
       Sep 11, 2015
    一般子类化 cell ,然后自行设置高亮和选中的效果
    strom001
        4
    strom001  
    OP
       Sep 12, 2015
    ```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
       Sep 12, 2015
    @PopeyeLau selectedBackgroundView 在 cellForItem 那里设置了也是 nil 啊……另外我在 IB 里设置了 cell 颜色,后来改为默认,结果代码设置颜色就不管用了……
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2513 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 06:09 · PVG 14:09 · LAX 23:09 · JFK 02:09
    ♥ Do have faith in what you're doing.