1

私はUICollectionViewに複数のセルがあり、そこに各セルの色を表すCAGradientレイヤーが追加されています。問題は、現在のView Controllerの上に別のView Controllerをプッシュしてから、2番目のView Controllerをポップすると、コレクションビューのセルがランダムな順序で色を移動することです。アイデアを伝えるためにスクリーンショットを添付しました。UICollectionViewCellが異常な動作をします。セルが正しくレイヤーを更新しない

これは、セルの元の順序です。これは私が別のビューコントローラをプッシュして、あなたは細胞が、私は何も変わっていないにもかかわらず、その色をシフトしていることがわかります This happens when I push another view controller and then return 戻ったときにこれが起こる This is the original order of the cells. This is correct

正しいです。

これは私がセルを初期化するために使用するコードです。

細胞からの眺めは、私が色を得る方法で、何も悪いことはありません

- (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; 
} 

、私は複数回をデバッグしていることをチェックして表示されるたびにロードして、[collectionview reloadData]を-viewWillAppearに呼び出されました私が得る色は正しいものです。私は

cell.backgroundColor = myColor; 

を行う場合、期待どおり 細胞は、その色や機能を変更しないでください。だから私はかなり問題がCAGradientLayerにあると確信しています。

私は考えることができるすべてを試しましたが、何も動作していないようです!

+0

は場合は '[event.activityLevel.activityName isEqualToString: "なし" @]'あなたは 'continue'いますが、ループ全体' NONE'を取得しない場合は何が起こりますか? – Lion

+0

次に、その下のif条件が実行されます。色はnilになり、私は灰色の陰影であるデフォルトの色を割り当てます。しかし、上記のスクリーンショットからわかるように、それはそうではありません。私は色の値を正しく取得しています。上記のように、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 docdequeueReusableCellWithReuseIdentifierを述べ、

コレクションビューに新しいセルを指定するよう求められたら、データソースオブジェクトからこのメソッドを呼び出します。このメソッドは、既存のセルが利用可能であれば、既存のセルをデキューします。または、以前登録したクラスまたは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

私はまだ最初に問題の原因を理解していませんが。セルを再ロードするたびにセルが作成されていますか?次に、レイヤーも作成されないのはなぜですか?または細胞がdequedされていますか?私はまだデキューのコンセプトに少し混乱している –

+0

ライオン!!!! hehehe ...:)あなたは大歓迎です! – Lion

関連する問題