简体   繁体   English

如何使用IBOutlet UIImageView将自定义对象添加到UICollectionViewCell?

[英]How to add a custom object with IBOutlet UIImageView to a UICollectionViewCell?

Basically I want to add custom object (GameBubble) with IBOutlet UIImageView (GameBubbleImageView) into my custom collection view cells (GameBubbleCell), However I cannot link the IBOutlet imageView to the controller. 基本上,我想将带有IBOutlet UIImageView(GameBubbleImageView)的自定义对象(GameBubble)添加到我的自定义集合视图单元格(GameBubbleCell)中,但是我无法将IBOutlet imageView链接到控制器。

GameBubble:
IBOutlet UIImageView *GameBubbleImageView;

GameBubbleCell:
GameBubble *bubble;

In the storyboard I can only find my GameBubbleCell, so I cannot link my GameImageView to the controller. 在情节提要中,我只能找到GameBubbleCell,因此无法将GameImageView链接到控制器。

My alternative way is to set: 我的另一种方法是设置:

GameBubble:
UIImage *GameBubbleImage;

GameBubbleCell:
IBOutlet UIImageView *GameBubbleImageView;
GameBubble *bubble;

but this is not a good MVC pattern. 但这不是一个好的MVC模式。 Is there a way to achieve my previous design? 有没有办法实现我以前的设计?

In storyboard drop your image view onto the collection view cell in collection view controller. 在情节提要中,将图像视图拖放到集合视图控制器中的集合视图单元格上。 Provide some integer value to the tag field of image view in the attributes inspector. 在属性检查器中为图像视图的标记字段提供一些整数值。 Following code might help you: 以下代码可能会帮助您:

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

Use Like this 像这样使用

GameBubble:
UIImageView *GameBubbleImageView;

GameBubbleCell:
IBOutlet GameBubble *bubble;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM