Objective c 如何从UICollectionView单元格设置UIImageView的背景色
我没有太多地使用UICollectionView,所以这可能不可能,但问一下也无妨:) 每个单元格设置为不同的颜色 我要做的是轻触一个单元格并推送到另一个UIViewController,它将包含与所选颜色相同的UIImageView 现在我可以更改第二个UIViewController视图的颜色,但不能更改其中UIImageView的颜色 以下是我的一些代码:Objective c 如何从UICollectionView单元格设置UIImageView的背景色,objective-c,uiimageview,uicollectionview,uicolor,uicollectionviewcell,Objective C,Uiimageview,Uicollectionview,Uicolor,Uicollectionviewcell,我没有太多地使用UICollectionView,所以这可能不可能,但问一下也无妨:) 每个单元格设置为不同的颜色 我要做的是轻触一个单元格并推送到另一个UIViewController,它将包含与所选颜色相同的UIImageView 现在我可以更改第二个UIViewController视图的颜色,但不能更改其中UIImageView的颜色 以下是我的一些代码: - (void)collectionView:(UICollectionView *)collectionView didSelec
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
[collectionView deselectItemAtIndexPath:indexPath animated:YES];
ImageViewController * imageVC = [[ImageViewController alloc] initWithNibName:@"ImageViewController" bundle:nil];
self.flatColor = [self colorForRow:indexPath.row];
[self.navigationController pushViewController:imageVC animated:YES];
imageVC.colorImageView.backgroundColor = _flatColor;
}
- (UIColor *)colorForRow:(NSInteger)row
{
UIColor *color;
switch (row)
{
case 0:
color = [UIColor redColor];
break;
case 2:
color = [UIColor greenColor];
break;
case 4:
color = [UIColor blueColor];
break;
default:
color = [UIColor yellowColor];
break;
}
return color;
}
编辑:
修正了这个问题
我不得不重新安排一些代码。需要首先完全加载新的viewController,然后更改UIImageView的背景色,请尝试以下代码
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
[collectionView deselectItemAtIndexPath:indexPath animated:YES];
ImageViewController * imageVC = [[ImageViewController alloc] initWithNibName:@"ImageViewController" bundle:nil];
self.flatColor = [self colorForRow:indexPath.row];
// This does not work
[imageVC.colorImageView setBackgroundColor:_flatColor];
//imageVC.colorImageView.backgroundColor = _flatColor;
// This works
//imageVC.view.backgroundColor = _flatColor;
[self.navigationController pushViewController:imageVC animated:YES];
}