Search code examples
iosiphonensmutablearrayuicollectionviewuicolor

Values in NSMutableArray seem to spontaneously change. How can this be?


I'm feeding background colors to the cells of a UICollectionView from an NSMutableArray, rainbowArray, which is populated with UIColors using this very elegant code from BJ Homer:

float INCREMENT = 0.04;

for (float hue = 0.0; hue < 1.0; hue += INCREMENT)
{
    UIColor *color = [UIColor colorWithHue:hue
                                saturation:1.0
                                brightness:1.0
                                     alpha:1.0];
    [rainbowArray addObject:color];
    NSLog(@"color in rainbow is %@",color);
}

Works great! NSLog'doutput proves the colors are there. However, when I go to actually apply the colors to my UICollectionViewCells, NSLogs demonstrate that the colors have somehow changed back to colorWithHue values of 1 0 0 1.

Here's the code from my cellForItemAtIndexPath, simplified because I'm sharing my delegate methods between two CVs:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    if (collectionView == self.usedColorsCollectionView)
    {
       ...        
    }


    else if (collectionView == self.pickerCollectionView)
    {
        static NSString *cellIdentifier = @"pickerCell";

        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];


        for (UIColor *thisColor in rainbowArray)
        {
            NSLog(@"Somehow color has changed to %@, and is being applied to cell",thisColor);

            [cell setBackgroundColor:thisColor];

            [cell.layer setCornerRadius:18.0f];
            [cell setUserInteractionEnabled:YES];

            return cell;
        }
    }
    return 0;
}

Here's the log, also simplified, first the values at the time they're created and inserted into the array:

2014-08-24 22:38:24.936 WMDGx[26662:90b] color in rainbow is UIDeviceRGBColorSpace 1 0 0 1
2014-08-24 22:38:24.937 WMDGx[26662:90b] color in rainbow is UIDeviceRGBColorSpace 1 0.24 0 1
2014-08-24 22:38:24.938 WMDGx[26662:90b] color in rainbow is UIDeviceRGBColorSpace 1 0.48 0 1
2014-08-24 22:38:24.939 WMDGx[26662:90b] color in rainbow is UIDeviceRGBColorSpace 1 0.72 0 1
2014-08-24 22:38:24.940 WMDGx[26662:90b] color in rainbow is UIDeviceRGBColorSpace 1 0.96 0 1
2014-08-24 22:38:24.940 WMDGx[26662:90b] color in rainbow is UIDeviceRGBColorSpace 0.8 1 0 1
2014-08-24 22:38:24.941 WMDGx[26662:90b] color in rainbow is UIDeviceRGBColorSpace 0.56 1 0 1
2014-08-24 22:38:24.941 WMDGx[26662:90b] color in rainbow is UIDeviceRGBColorSpace 0.32 1 0 1

...

Here are the values at the time they're applied to the cells:

2014-08-24 22:38:24.959 WMDGx[26662:90b] Somehow color has changed to UIDeviceRGBColorSpace 1 0 0 1, and is being applied to cell
2014-08-24 22:38:24.960 WMDGx[26662:90b] Somehow color has changed to UIDeviceRGBColorSpace 1 0 0 1, and is being applied to cell
2014-08-24 22:38:24.961 WMDGx[26662:90b] Somehow color has changed to UIDeviceRGBColorSpace 1 0 0 1, and is being applied to cell
2014-08-24 22:38:24.961 WMDGx[26662:90b] Somehow color has changed to UIDeviceRGBColorSpace 1 0 0 1, and is being applied to cell
2014-08-24 22:38:24.962 WMDGx[26662:90b] Somehow color has changed to UIDeviceRGBColorSpace 1 0 0 1, and is being applied to cell
2014-08-24 22:38:24.963 WMDGx[26662:90b] Somehow color has changed to UIDeviceRGBColorSpace 1 0 0 1, and is being applied to cell
2014-08-24 22:38:24.964 WMDGx[26662:90b] Somehow color has changed to UIDeviceRGBColorSpace 1 0 0 1, and is being applied to cell
2014-08-24 22:38:24.964 WMDGx[26662:90b] Somehow color has changed to UIDeviceRGBColorSpace 1 0 0 1, and is being applied to cell
2014-08-24 22:38:24.965 WMDGx[26662:90b] Somehow color has changed to UIDeviceRGBColorSpace 1 0 0 1, and is being applied to cell
2014-08-24 22:38:24.965 WMDGx[26662:90b] Somehow color has changed to UIDeviceRGBColorSpace 1 0 0 1, and is being applied to cell
2014-08-24 22:38:24.966 WMDGx[26662:90b] Somehow color has changed to UIDeviceRGBColorSpace 1 0 0 1, and is being applied to cell

And here's a screenshot of the result:

enter image description here

The cells in question are the big red ones, which should be multicolored.

I've been trying to figure this out for hours. Any ideas?

Edit:

All fixed! I used the code supplied by @Paulw11, but thanks also to @Shan and @Ramshad for some syntax I'll have to read up on as it was unfamiliar to me.

It looks like this now:

enter image description here

after I added this code (posted by @neilsbot in this answer) to flesh out the array of colors, which now contains 68 color wells.

float INCREMENT = 0.06;

for (float hue = 0.0; hue < 1.0; hue += INCREMENT)
{
    UIColor *color = [UIColor colorWithHue:hue
                                saturation:1.0
                                brightness:1.0
                                     alpha:1.0];

    CGFloat oldHue, saturation, brightness, alpha ;

    BOOL gotHue = [color getHue:&oldHue saturation:&saturation brightness:&brightness alpha:&alpha ];

    if (gotHue)
    {
        UIColor * newColor = [ UIColor colorWithHue:hue saturation:0.7 brightness:brightness alpha:alpha ];
        UIColor * newerColor = [ UIColor colorWithHue:hue saturation:0.5 brightness:brightness alpha:alpha ];
        UIColor * newestColor = [ UIColor colorWithHue:hue saturation:0.3 brightness:brightness alpha:alpha ];

        [rainbowArray addObject:color];
        [rainbowArray addObject:newColor];
        [rainbowArray addObject:newerColor];
        [rainbowArray addObject:newestColor];
    }
}

Solution

  • You have a return in your for loop, so you are always returning the first element from the array, which is 1 0 0 1.

    What you want is

    else if (collectionView == self.pickerCollectionView) {
            static NSString *cellIdentifier = @"pickerCell";
    
            UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    
            [cell setBackgroundColor:[rainbowArray objectAtIndex:indexPath.item]];
    
            [cell.layer setCornerRadius:18.0f];
            [cell setUserInteractionEnabled:YES];
    
             return cell;
     }