ios - 更改 UICollectionView 中特定单元格的图像

标签 ios objective-c xcode uicollectionview

我在 UICollectionView 中有一个 5x5 的单元格网格,每个单元格都有这样的图像集:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CollectionViewCell *cell = (CollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"circleCell" forIndexPath:indexPath];

    imageArray = [[NSMutableArray alloc]init];
    for (int i=1; i<=12; i++) {
        UIImage *imageToAdd = [UIImage imageNamed:[NSString stringWithFormat:@"%i", i]];
        [imageArray addObject:imageToAdd];
    }

    self.cellImageView = [[UIImageView alloc] initWithFrame:[self getCircleSize]];
    int i = arc4random() % 12;
    [self.cellImageView setImage:[imageArray objectAtIndex:i]];
    [cell addSubview:self.cellImageView];
    cell.tag = i;
    return cell;
}

每张图片都是一个随机颜色的圆圈 - 1.png 到 12.png。 我正在检测特定单元格上的点击,然后在 Collection View 中获取它的颜色和位置:

UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
NSIndexPath *indexPath = [collectionView indexPathForCell:cell];

self.firstCircleSection = indexPath.section;
self.firstCircleRow = indexPath.row;
self.firstCircleIndex = (indexPath.section * 5) + (indexPath.row + 1);

self.firstCircleNum = (int)cell.tag;
//I have a dictionary with colors and keys, e.g 1 Yellow, 2 Blue, 3 Red. This line gets the color:
NSString *dotColor = [NSString stringWithFormat:@"Colour: %@", [self.colorDict objectForKey:[NSString stringWithFormat:@"%ld", self.firstCircleNum]]];

然后对于另一个圆的第二次圆点击,我正在计算每个圆的新颜色(两种选定颜色中间的颜色):

UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
NSIndexPath *indexPath = [collectionView indexPathForCell:cell];

self.secondCircleSection = indexPath.section;    
self.secondCircleRow = indexPath.row;    
self.secondCircleIndex = (indexPath.section * 5) + (indexPath.row + 1);
self.secondCircleNum = (int)cell.tag;

NSString *circleColor = [NSString stringWithFormat:@"Colour: %@", [self.colorDict objectForKey:[NSString stringWithFormat:@"%ld", self.secondCircleNum]]];

NSInteger i = (self.firstCircleNum + self.secondCircleNum)/2;
NSString *dotColor = [NSString stringWithFormat:@"Colour: %@", [self.colorDict objectForKey:[NSString stringWithFormat:@"%ld", i]]];

// Labels on the view for showing what the colors mix like
colorLabel2.text = circleColor;
positionLabel2.text = [NSString stringWithFormat: @"Position: %ld", self.secondCircleIndex];
mixedColorLabel.text = [NSString stringWithFormat:@"New %@", dotColor];

NSIndexPath *indexPath2 = [NSIndexPath indexPathForRow:self.firstCircleRow inSection:self.firstCircleSection];
NSInteger theFirstCircleIndex = (indexPath2.section * 5) + (indexPath2.row + 1);
NSIndexPath *indexPath3 = [NSIndexPath indexPathForRow:self.secondCircleRow inSection:self.secondCircleSection];
NSInteger theSecondCircleIndex = (indexPath3.section * 5) + (indexPath3.row + 1);

// Get a reference to the two cells
UICollectionViewCell *firstCircle = [collectionView dequeueReusableCellWithReuseIdentifier:@"circleCell" forIndexPath:indexPath2];
UICollectionViewCell *secondCircle = [collectionView dequeueReusableCellWithReuseIdentifier:@"circleCell" forIndexPath:indexPath3];

我的问题是,完成所有这些操作后,如何更改这两个单元格(firstCircle 和 secondCircle)的图像?我在自定义 CollectionViewCell、cellImage 上有一个属性,但无法通过以下方式访问它:firstCircle.cellImage。我想更改两个单元格上的图像。

最佳答案

在您的 cellForItemAtIndexPath: 方法中,您正在创建一个新的 UIImageView,从您的数组中设置其图像,并将其添加为单元格的 subview 。这与单元格的 cellImage 属性不同。要解决此问题:

要么: 如果您已经在 Storyboard中配置了自定义 CollectionViewCell(即将 ImageView 添加到原型(prototype)单元格,并将其连接到 CollectionViewCell 的 cellImage 导出),那么您不需要不要添加额外的 UIImageView,您可以使用:

cell.cellImage.image = [imageArray objectAtIndex:i];

或者: 如果它没有在 Storyboard(或 CollectionViewCell 初始化程序)中配置,则保留您现有的代码,但将 cell.cellImage 设置为指向您创建的 imageView:

cell.cellImage = self.cellImageView;

下一步: 在您的其他代码中(我假设它在 didSelectItemAtIndexPath 中?),以下行告诉编译器单元格属于 UICollectionViewCell 基类:

UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];

但是您希望编译器知道这些单元格实际上是您自定义的 CollectionViewCell 类。所以将行修改为:

CollectionViewCell *cell = (CollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];

(其他地方也类似)。完成后,编译器应该允许您毫无问题地使用 cellImage 属性。

现在,要获取对 indexPath2indexPath3 的单元格的引用,请不要使用 dequeueReusableCellWithIdentifier: 方法,请使用 cellForItemAtIndexPath:(就像您在第一行所做的那样):

CollectionViewCell *firstCircle = [collectionView cellForItemAtIndexPath:indexPath2];
CollectionViewCell *secondCircle = [collectionView cellForItemAtIndexPath:indexPath3];

然后您可以使用 firstCircle.cellImage.image 等操作他们的图像

此外,您的代码中存在一些冗余:这些行:

UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
NSIndexPath *indexPath = [collectionView indexPathForCell:cell];

您正在获取给定 indexPath 的单元格,然后获取该单元格的 indexPath - 它们将是相同的。所以你可以删除这些行中的第二行。此外,在处理 secondCircle 的代码中,indexPath3indexPath 相同,而 secondCircle 实际上是cell 您在代码开头设置的。所以只需使用:

CollectionViewCell *secondCircle = cell;

最后,您的 cellForItemAtIndexPath 每次调用时都会加载 imageArray - 这是不必要的。我会使用不同的方法来加载这些图像 - 也许是 viewDidLoad 方法。

关于ios - 更改 UICollectionView 中特定单元格的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26559071/

相关文章:

Objective-C 相当于 'tuple unpacking'

iphone - 从后台获取位置时 Location.Horizo​​ntalAccuracy 太差

iphone - iOS 崩溃并出现 EXC_BAD_ACCESS(code=1)

ios - 如何选择要在 Nativescript 中使用的 Swift 编译器版本?

ios - Storyboard连接检查器中的方法名称发生更改

iPhone - URL 有效性的 Reg Exp

iphone - 在iphone中将图像发布到服务器

ios - 使用 UICollectionView header 中的按钮进行操作

ios - 使用 Firebase 每天执行一次 Swift 函数

iphone - 如何将黑色变为白色