NSMutableArray中的值似乎会自发地发生变化。怎么会这样?

时间:2014-08-25 06:21:06

标签: ios iphone nsmutablearray uicollectionview uicolor

我正在使用此very elegant code from BJ HomerUICollectionView NSMutableArray的{​​{1}}的单元格提供背景颜色,rainbowArray填充UIColors

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

效果很好! NSLog'd输出证明颜色在那里。但是,当我实际将颜色应用到我的UICollectionViewCells时,NSLogs会证明颜色已经以某种方式更改回colorWithHue 1 0 0 1的值。

这是我的cellForItemAtIndexPath中的代码,简化了,因为我在两个CV之间共享我的委托方法:

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

这是日志,也是简化的,首先是它们创建并插入数组时的值:

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

...

以下是它们应用于单元格时的值:

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

这是结果的截图:

enter image description here

有问题的细胞是红色的,应该是多色的。

我一直试图弄清楚这几个小时。有什么想法吗?

修改

全部修好了!我使用了@ Paulw11提供的代码,但是感谢@Shan和@Ramshad的一些语法,我将不得不阅读,因为我不熟悉。

现在看起来像这样:

enter image description here

我添加了这段代码(posted by @neilsbot in this answer)以充实颜色数组,现在包含68个颜色的孔。

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

2 个答案:

答案 0 :(得分:3)

你的for循环中有一个return,所以你总是返回数组中的第一个元素,即1 0 0 1

你想要的是

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

答案 1 :(得分:0)

cellForItemAtIndexPath将触发每个单元格。因此,只需使用indexPath.row % rainbowArray.count作为数组索引即可获得颜色。另外,请确保从循环中删除return语句。

cellForItemAtIndexPath修改为

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

        UIColor *thisColor = [rainbowArray objectAtIndex:(indexPath.row % rainbowArray.count)]; //this will repeat the colors from begning if they end.

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

   return cell;
}