0

設置一個UIImageView的背景顏色我沒有用太多UICollectionView所以這是不可能的,但從來沒有傷害要求:)如何從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

回答

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]; 
} 
+0

仍然不工作:( –