我有一個UICollectionView
多個單元格,我在其上添加了代表每個單元格顏色的CAGradient
圖層。問題是,當我推動當前視圖控制器頂部的另一個視圖控制器,然後彈出第二個視圖控制器時,我的集合視圖單元格以隨機順序移動顏色。給你一個想法,我附上了截圖。UICollectionViewCell奇怪的行爲。沒有正確更新圖層的單元格
發生這種情況時,我把另一個視圖控制器,然後返回 你可以看到細胞轉移了他們的顏色,即使我什麼也沒有改變。
這是我用來初始化單元的代碼。
[的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。
我試過了所有我能想到的東西,但似乎沒有任何工作!
如果'[event.activityLevel.activityName isEqualToString:@ 「無」]'那麼你'continue'但是如果你不能得到'整個循環none'發生的呢? – Lion
然後下面的if條件被執行,因爲顏色將是零,我將分配默認顏色,這是一個灰色陰影。但是從上面的截圖可以看出,情況並非如此。我正確地獲取顏色值。就像我提到的,如果我做cell.backgroundColor = myColor,單元格按照預期工作。當我使用CAGradientLayer –
嘗試我的答案時,它可能會發生問題! – Lion