我在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。 我正在检测特定单元格上的水龙头,然后在集合视图中获取它的颜色和位置:
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访问它。我想改变两个单元格上的图像。
答案 0 :(得分:2)
在cellForItemAtIndexPath:
方法中,您要创建一个新的UIImageView
,从阵列中设置其图像,并将其添加为单元格的子视图。这与单元格的cellImage
属性不同。解决这个问题:
<强>或者:强>
如果您已在故事板中配置自定义CollectionViewCell
(即将图像视图添加到原型单元格,并将其连接到CollectionViewCell的cellImage
插座),那么您不需要&#39;添加您可以使用的额外UIImageView
:
cell.cellImage.image = [imageArray objectAtIndex:i];
或强> 如果未在故事板(或CollectionViewCell初始化程序)中配置,则保留现有代码,但将cell.cellImage设置为指向您创建的imageView:
cell.cellImage = self.cellImageView;
下一步:强>
在您的其他代码中(我假设它位于didSelectItemAtIndexPath
?)中,以下行告诉编译器单元格是基类UICollectionViewCell
类:
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
但是您希望编译器知道这些单元实际上是您的自定义CollectionViewCell类。所以将该行修改为:
CollectionViewCell *cell = (CollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
(和其他地方一样)。完成后,编译器应该允许您毫无问题地使用cellImage
属性。
现在,要获取indexPath2
和indexPath3
的单元格的引用,请不要使用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
的代码中,indexPath3
与indexPath
相同,而secondCircle
实际上是您在开始时设置的cell
代码。所以只需使用:
CollectionViewCell *secondCircle = cell;
最后,你的cellForItemAtIndexPath
每次调用时都会加载imageArray - 这是不必要的。我会使用不同的方法来加载这些图像 - 也许是viewDidLoad
方法。