UICollectionViewCell奇怪的行为。没有正确更新图层的单元格

问题描述:

我有一个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

试试这一次,

[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