gpt4 book ai didi

ios - NSMutableArray 中的值似乎会自发地改变。怎么会这样?

转载 作者:行者123 更新时间:2023-11-28 19:57:55 26 4
gpt4 key购买 nike

我正在从 NSMutableArrayrainbowArrayUICollectionView 的单元格提供背景颜色,其中填充了 UIColors 使用这个 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);
}

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

这是来 self 的 cellForItemAtIndexPath 的代码,经过简化是因为我在两个 CV 之间共享我的委托(delegate)方法:

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

最佳答案

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

关于ios - NSMutableArray 中的值似乎会自发地改变。怎么会这样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25480034/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com