代码之家  ›  专栏  ›  技术社区  ›  rattletrap99

NSMutableArray中的值似乎会自动更改。这怎么可能?

  •  0
  • rattletrap99  · 技术社区  · 10 年前

    我正在给一个 UICollectionView 来自 NSMutableArray , rainbowArray ,其中填充了 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 .

    这是我的代码 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 回复  |  直到 7 年前
        1
  •  3
  •   Paulw11    10 年前

    你有一个 return 在for循环中,因此总是返回数组中的第一个元素,即 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;
     }
    
        2
  •  0
  •   Shamsudheen TK    10 年前

    cellForItemAtIndexPath 将为每个单元触发。所以只要使用 indexPath.row % rainbowArray.count 作为数组索引以获取颜色。此外,确保拆下 return 语句。

    修改您的 项目索引路径单元格

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