2014-09-23 50 views
0

過程中添加我添加了一個以collectionView cell的選擇和重新加載項目,但認爲不在第一選擇可見,其只有再次單擊單元格。後可見第一次加載collectionView時,首先選擇collectionView單元格,添加的視圖被添加但不可見,大小增加,只有第二次選擇後視圖纔可見。有何看法加入到uicollectionview細胞沒有出現該選擇

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ 
selectedIndexPath = indexPath; 
    PastPicksGameCollectionViewCell *cell = (PastPicksGameCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath]; 
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(50,122, 150, 120)]; 
[view setBackgroundColor:[UIColor blueColor]]; 
[cell.contentView addSubview:view] 
    [pastPicksGameCollectionView reloadItemsAtIndexPaths:@[selectedIndexPath]]; 
} 

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { 
if ([indexPath isEqual:selectedIndexPath]) { 
    return CGSizeMake(320, 200); 
} 
return CGSizeMake(300, 120); 
} 

回答

0

我覺得reloadItemsAtIndexPaths:工作對你:它導致的CollectionView放棄與該項目相關聯的小區,以及從隊列中加載新的小區(即你剛纔添加的子視圖到細胞)(其該項目沒有子視圖)。丟棄的單元被放置在隊列中以供重新使用。當你再次點擊時,同樣的事情發生。但是這一次,最初丟棄的單元(它具有子視圖)被重新使用,因此被顯示。我個人會避免將子視圖添加到像這樣的單元格中 - 我會爲您的自定義單元格類添加一個「永久」UIView屬性,然後您可以在didSelectItemAtIndexPath中使用它。但是您必須確保在cellForItemAtIndexPath中您重置了它(例如,使用if (indexPath isEqual:selectedIndexPath) { create/show the subview } else { hide the subview/set to nil) }

相關問題