【问题标题】:How to set background color of a UIImageView from a UICollectionView cell如何从 UICollectionView 单元格设置 UIImageView 的背景颜色
【发布时间】:2013-05-31 16:00:12
【问题描述】:

我没有经常使用 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

【问题讨论】:

    标签: objective-c uiimageview uicollectionview uicolor uicollectionviewcell


    【解决方案1】:

    试试下面的代码。

    - (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];
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-21
      • 2014-06-29
      • 1970-01-01
      • 2017-02-08
      • 2016-12-18
      • 1970-01-01
      • 2017-12-08
      • 2015-06-02
      相关资源
      最近更新 更多