0

我没有太多使用 UICollectionView ,所以这可能是不可能的,但问起来永远不会有害:)

每个单元格都设置为不同的颜色。

我想要做的是点击一个单元格并推送到另一个 UIViewController,它将包含一个与所选颜色相同的 UIImageView。

现在我可以更改第二个 UIViewController 视图的颜色,但不能更改其中的 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];

     [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的backgroundColor

4

1 回答 1

0

试试下面的代码。

- (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];
 }
于 2013-06-05T04:48:16.910 回答