【问题标题】:How to add a custom object with IBOutlet UIImageView to a UICollectionViewCell?如何将带有 IBOutlet UIImageView 的自定义对象添加到 UICollectionViewCell?
【发布时间】:2014-02-05 08:22:22
【问题描述】:

基本上我想将带有 IBOutlet UIImageView (GameBubbleImageView) 的自定义对象 (GameBubble) 添加到我的自定义集合视图单元 (GameBubbleCell) 中,但是我无法将 IBOutlet imageView 链接到控制器。

GameBubble:
IBOutlet UIImageView *GameBubbleImageView;

GameBubbleCell:
GameBubble *bubble;

在故事板中我只能找到我的 GameBubbleCell,所以我无法将我的 GameImageView 链接到控制器。

我的替代方法是设置:

GameBubble:
UIImage *GameBubbleImage;

GameBubbleCell:
IBOutlet UIImageView *GameBubbleImageView;
GameBubble *bubble;

但这不是一个好的 MVC 模式。有没有办法实现我之前的设计?

【问题讨论】:

  • 根据 .h 和 .m 文件添加完整代码 - 代码不够清晰,无法回答
  • 没有代码很难得到你想要做的事情。如果我理解正确,您正在尝试将自定义类添加到您的视图控制器。这仅在您的自定义类继承自可放入视图控制器的对象(例如 UIView、UIImageView、UITableViewCell 等)时才有效。

标签: ios objective-c uiimageview uicollectionview uicollectionviewcell


【解决方案1】:

在情节提要中,将您的图像视图拖放到集合视图控制器中的集合视图单元格上。 在属性检查器中为图像视图的标签字段提供一些整数值。 以下代码可能会对您有所帮助:

-(UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *identifier = @"Cell";
    UICollectionViewCell *collectionViewCell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    UIImageView *recipeImageView = (UIImageView *) [collectionViewCell viewWithTag:100];
    recipeImageView.image = [UIImage imageNamed:[recipePhotos objectAtIndex:indexPath.row]];
    //collectionViewCell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"photo-frame.png"]];
    return collectionViewCell;
}

【讨论】:

    【解决方案2】:

    这样使用

    GameBubble:
    UIImageView *GameBubbleImageView;
    
    GameBubbleCell:
    IBOutlet GameBubble *bubble;
    

    【讨论】:

      猜你喜欢
      • 2012-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-20
      • 2012-12-01
      • 1970-01-01
      • 2023-03-26
      相关资源
      最近更新 更多