UICollectionView <一> 選中Cell那些事兒

UICollectionView 是最常用的控件,UITableView也是定制過(guò)的UICOllectionView。所以說(shuō),拓展度是最高的,很多UITableView中的效果在UICollectionView中沒有特定的屬性,比如選中效果。

tableView選中效果設(shè)置

tableview中,選中效果可以通過(guò)設(shè)置屬性來(lái)是實(shí)現(xiàn)。

@property(nonatomic) UITableViewCellSelectionStyle selectionStyle

typedef enum : NSInteger {
   UITableViewCellSelectionStyleNone, // 無(wú)選中效果
   UITableViewCellSelectionStyleBlue, // 藍(lán)色選中效果 
   UITableViewCellSelectionStyleGray, // 灰色選中效果
   UITableViewCellSelectionStyleDefault // 沒試驗(yàn)過(guò)的選中效果
}  UITableViewCellSelectionStyle;

如果想要點(diǎn)擊選中,松手取消選中的狀態(tài),在點(diǎn)擊的代理方法中調(diào)用
- deselectRowAtIndxPath:animated: 方法即可。

collectionView選中效果

上面說(shuō)到tableView是collectionView的定制,所以collectionView也是有選中方法設(shè)置的。

// 返回 YES 允許顯示高亮 NO 不允許顯示高亮狀態(tài)
- (BOOL)collectionView:(UICollectionView **) collectionView 
shouldHighlightItemAtIndexPath:(NSIndexPath *)*indexPath*
// 設(shè)置點(diǎn)擊時(shí) 觸發(fā)的高亮狀態(tài)方法,可在此代理方法中進(jìn)行高亮狀態(tài)的處理。例如高亮顏色的變換。
- (void)collectionView : (UICollectionView *)*collectionView* 
didHighlightItemAtIndexPath : (NSIndexPath *)*indexPath*
// 設(shè)置點(diǎn)擊結(jié)束時(shí)(即點(diǎn)擊后松開狀態(tài))觸發(fā)。
- (void)collectionView:(UICollectionView *)*collectionView* 
didUnhighlightItemAtIndexPath : (NSIndexPath *)*indexPath*

當(dāng)我們想改變cell點(diǎn)擊和松開的顏色時(shí),可以修改 cell的 backgroundColor屬性。設(shè)置cell的背景色使用contentView.backgroundColor。
區(qū)別:backgroundColor是UIView的方法。就是承載cell的view。
contentView.backgroundColor是cell視圖的顏色

我猜測(cè)當(dāng)點(diǎn)擊事件觸發(fā)的時(shí)候,會(huì)把cell的contentVeiw的顏色清空,這樣才會(huì)顯示在最底層的View的顏色,即view的顏色。

通過(guò)上面介紹的代理方法和屬性,即可自定義設(shè)置點(diǎn)擊collectionView的點(diǎn)擊顏色了。

注意事項(xiàng):

在實(shí)際開發(fā)中,為了讓點(diǎn)擊效果出現(xiàn)后消失,我們都會(huì)使用以下兩種方法
- (void)collectionView : (UICollectionView *)collectionView
didSelectItemAtIndexPath : (NSIndexPath *)indexPath
- (void)collectionView : (UICollectionView *)collectionView
didDeselectItemAtIndexPath : (NSIndexPath *)indexPath

在點(diǎn)擊的代理方法中調(diào)用取消點(diǎn)擊方法。
然而如果你實(shí)現(xiàn)了 本文介紹的 這個(gè)兩個(gè)方法改變選中顏色,你會(huì)發(fā)現(xiàn)只有長(zhǎng)按時(shí)才會(huì)看到設(shè)置的顏色。
原因:長(zhǎng)按沒有松手的時(shí)候,觸發(fā)的是高亮方法,松手觸發(fā)的是取消高亮的方法。輕觸點(diǎn)擊的時(shí)候會(huì)很快速的響應(yīng) 高亮和取消高亮的方法,所以看不到顏色的改變。此時(shí),需要設(shè)置delaysContentTouches屬性為NO,此時(shí)當(dāng)點(diǎn)擊的時(shí)候會(huì)立刻調(diào)用點(diǎn)擊事件的begin方法,率先變成高亮狀態(tài)。

@property(nonatomic) BOOL delaysContentTouches; // default is YES. if NO, 
//we immediately call -touchesShouldBegin:withEvent:inContentView:. 
//this has no effect on presses

本文涉及重要的相關(guān)API:

UITableView
@property(nonatomic) UITableViewCellSelectionStyle selectionStyle
UICollectionView
@property(nonatomic) BOOL delaysContentTouches
- (BOOL)collectionView:(UICollectionView ) collectionView
shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath
- (void)collectionView : (UICollectionView )collectionView
didHighlightItemAtIndexPath : (NSIndexPath )indexPath
-(void)collectionView:(UICollectionView )collectionView
didUnhighlightItemAtIndexPath : (NSIndexPath )indexPath

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容