1

我有一個UICollectionView多個單元格,我在其上添加了代表每個單元格顏色的CAGradient圖層。問題是,當我推動當前視圖控制器頂部的另一個視圖控制器,然後彈出第二個視圖控制器時,我的集合視圖單元格以隨機順序移動顏色。給你一個想法,我附上了截圖。UICollectionViewCell奇怪的行爲。沒有正確更新圖層的單元格

這是單元格的原始順序。這是正確的 This is the original order of the cells. This is correct

發生這種情況時,我把另一個視圖控制器,然後返回 This happens when I push another view controller and then return 你可以看到細胞轉移了他們的顏色,即使我什麼也沒有改變。

這是我用來初始化單元的代碼。

[的CollectionView reloadData]被稱爲-viewWillAppear使細胞加載每次出現的視圖

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [self filterRecords]; 
    MyCell *cell = (MyCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"ProspectCVCell" forIndexPath:indexPath]; 


    for (int i = 0; i < [eventsSortedForAddedDate count]; i++) 
    { 
     Events* event = (Events*)[eventsSortedForAddedDate objectAtIndex:i]; 

     if ([event.activityLevel.activityName isEqualToString:@"None"]) 
      continue; 

     color = [[event activityLevel] color]; 

     if (![color isEqualToString:@"#FFCCCCCC"]) 
      break; 
     else 
      color = nil; 
    } 


    if (!([color length] > 0) || color == nil) 
    { 
     color = @"#FFCCCCCC"; 
    } 

    UIColor* myColor = [self getUIColorObjectFromHexString:color alpha:.9]; 

    //cell.backgroundColor = myColor; 

    CAGradientLayer *gradient = [CAGradientLayer layer]; 
    gradient.frame = cell.bounds; 
    gradient.colors = [NSArray arrayWithObjects:(id)[myColor CGColor], (id)[[UIColor whiteColor] CGColor], nil]; 
    gradient.locations = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0f], [NSNumber numberWithFloat:0.85f], nil]; 
    //[cell.layer insertSublayer:gradient atIndex:0]; 
    [cell.layer insertSublayer:gradient atIndex:0]; 


    cell.prospectImageView.layer.shadowColor = [UIColor blackColor].CGColor; 
    cell.prospectImageView.layer.shadowOffset = CGSizeMake(0, 3.0); 
    cell.prospectImageView.layer.shadowRadius = 3.0; 
    cell.prospectImageView.layer.shadowOpacity = 0.8; 

    cell.cellLabel.text = p.displayName; 

    cell.layer.borderWidth = 0.5f; 
    cell.layer.borderColor = [UIColor whiteColor].CGColor; 

    return cell; 
} 

沒有什麼錯,我得到的顏色的方式,我已經調試多次,檢查了我得到的顏色是正確的。 如果我做

cell.backgroundColor = myColor; 

如預期的細胞不改變它們的顏色和功能。所以我很確定問題在於CAGradientLayer。

我試過了所有我能想到的東西,但似乎沒有任何工作!

+0

如果'[event.activityLevel.activityName isEqualToString:@ 「無」]'那麼你'continue'但是如果你不能得到'整個循環none'發生的呢? – Lion

+0

然後下面的if條件被執行,因爲顏色將是零,我將分配默認顏色,這是一個灰色陰影。但是從上面的截圖可以看出,情況並非如此。我正確地獲取顏色值。就像我提到的,如果我做cell.backgroundColor = myColor,單元格按照預期工作。當我使用CAGradientLayer –

+1

嘗試我的答案時,它可能會發生問題! – Lion

回答

1

試試這一次,

[cell.layer insertSublayer:gradient atIndex:[[cell.layer sublayers] indexOfObject:[[cell.layer sublayers] lastObject]]]; 

,而不是

[cell.layer insertSublayer:gradient atIndex:0]; 

更新:

至於問評論,Apple doc州約dequeueReusableCellWithReuseIdentifier

當要求爲集合視圖提供新單元時,請從數據源對象中調用此方法。此方法將取出現有單元格(如果有),或根據您之前註冊的類或nib文件創建新單元格。

And second thing you can also remove previous layers and then can add new one at index 0. it is better because it not increase number of layer which is not necessary.

+0

謝謝!這就是爲什麼他們稱你爲獅子的原因:D它讓我在過去的8個小時裏撓頭。它現在完美。 –

+0

雖然我還是不明白是什麼原因造成了這個問題。每次我重新加載時,單元格都被創建了嗎?那麼爲什麼還沒有創建圖層。或者這些細胞正在被馴服?我仍然有點混淆概念出列 –

+0

獅子! hehehe ...):不客氣! – Lion

相關問題