简体   繁体   English

UICollectionView设置特定位置indexPath.row的背景

[英]UICollectionView Set Background for Specific Position indexPath.row

Fairly new to iOS working on weather App utilizing UICollectionView in which the coldest hour of forecast will have blue background gradient, and warmest hour will have orange background gradient. 对于使用UICollectionView的天气应用程序的iOS来说,这是相当新的东西,其中最冷的小时将具有蓝色背景渐变,而最暖的小时将具有橙色背景渐变。

The logic I've created is I identify the position in my array of coldest and warmest, then match that up to the indexPath.row to set the appropriate gradient. 我创建的逻辑是确定数组中最冷和最暖的位置,然后将其匹配到indexPath.row以设置适当的渐变。 However, it only works one time. 但是,它只能运行一次。 When I scroll or switch views it seems the gradients are randomly assigned. 当我滚动或切换视图时,渐变似乎是随机分配的。 When I NSLog both the low/high temp and indexPath.row, they seem to equal as expected. 当我同时记录低温/高温和indexPath.row时,它们似乎都与预期相等。

Here is my code: 这是我的代码:

 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"ConditionsCell";

ConditionsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

cell.conditionsTime.text = [self.hours objectAtIndex:indexPath.row];
cell.conditionsTemp.text = [NSString stringWithFormat:@"%@°", [self.hoursTemp objectAtIndex:indexPath.row]];
cell.conditionsImage.image = [UIImage imageNamed:@""]; //resetting image

NSLog (@"indexPath %d self.lowTempPosition %d", indexPath.row, self.lowTempPosition);

if(indexPath.row == self.lowTempPosition)
{
CAGradientLayer *gradientBlue = [CAGradientLayer layer];
gradientBlue.frame = cell.bounds;
gradientBlue.colors = [NSArray arrayWithObjects:(id)[[Utils beginBlue] CGColor], (id)[[Utils endBlue] CGColor], nil];
[cell.contentView.layer insertSublayer:gradientBlue atIndex:0];
}
else if (indexPath.row == self.highTempPosition)
{
    CAGradientLayer *gradientBlue = [CAGradientLayer layer];
    gradientBlue.frame = cell.bounds;
    gradientBlue.colors = [NSArray arrayWithObjects:(id)[[Utils beginOrange] CGColor], (id)[[Utils endOrange] CGColor], nil];
    [cell.contentView.layer insertSublayer:gradientBlue atIndex:0];
}
else
{
    CAGradientLayer *gradientWhite = [CAGradientLayer layer];
    gradientWhite.frame = cell.bounds;
    gradientWhite.colors = [NSArray arrayWithObjects:(id)[[UIColor whiteColor] CGColor], (id)[[UIColor whiteColor] CGColor], nil];
    [cell.ContentView.layer insertSublayer:gradientWhite atIndex:0];
}

     NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithURL:[NSURL URLWithString:[self.hoursIcons objectAtIndex:indexPath.row]] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    @autoreleasepool {//autorelease pool for memory release
        if (!error) {

            dispatch_async(dispatch_get_main_queue(), ^{
                cell.conditionsImage.image = [UIImage imageWithData: data];//update UI

            });

        }}//autorelease pool

}];


[dataTask resume];

return cell;
 }

The problem is because of the reuse of the cells. 问题是由于单元的重用。 You are adding the gradient as a sublayer to the cell contentView. 您正在将渐变作为子层添加到单元格contentView中。 So on scrolling, the sublayers still exist. 因此,在滚动时,子层仍然存在。 Like you are resetting the image, you should reset the layers too. 就像重置图像一样,您也应该重置图层。 The fastest way to do is to call: 最快的方法是致电:

cell.contentView.layer.sublayers = nil;

This should reset the layers and then you can add your desired temperature gradient again. 这将重置图层,然后您可以再次添加所需的温度梯度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM