Ios Imageview未显示在以编程方式创建的collectionview中

Ios Imageview未显示在以编程方式创建的collectionview中,ios,objective-c,uiimageview,uicollectionview,Ios,Objective C,Uiimageview,Uicollectionview,我很难弄明白为什么cell.imageview属性没有在下面显示imageUIImage不是零,创建了单元格,如果我为单元格指定了背景,它将显示出来,这证明我正在创建collectionview并且我的单元格是正确的。我以编程方式创建我的集合视图,不使用任何IB或接口生成器。我以前做过很多次,但这一次真的让我抓狂 提前谢谢 @interface MainViewController : UIViewController @property(strong, nonatomic)

我很难弄明白为什么
cell.imageview
属性没有在下面显示
image
UIImage
不是零,创建了单元格,如果我为单元格指定了背景,它将显示出来,这证明我正在创建
collectionview
并且我的单元格是正确的。我以编程方式创建我的
集合视图
,不使用任何IB或
接口生成器
。我以前做过很多次,但这一次真的让我抓狂

提前谢谢

    @interface MainViewController : UIViewController

    @property(strong, nonatomic) UICollectionView *collectionView;

    @end

    @interface MainViewController ()<
    UICollectionViewDataSource, UICollectionViewDelegate,
    UICollectionViewDelegateFlowLayout, UIGestureRecognizerDelegate>

    @property(nonatomic, strong) MHTransitionShowDetail *interactivePushTransition;
    @property(nonatomic) CGPoint lastPoint;
    @property(nonatomic) CGFloat startScale;
    @property(strong, nonatomic) NSMutableArray *carImages;

    @end

    @implementation MainViewController

    - (instancetype)init {
      self = [super init];
      if (self) {
      }
      return self;
    }

    - (void)viewDidLoad {
      [super viewDidLoad];
      // Do any additional setup after loading the view, typically from a nib.

      _carImages = [@[
        @"chevy_small",
        @"mini_small",
        @"rover_small",
        @"smart_small",
        @"highlander_small",
        @"venza_small",
        @"volvo_small",
        @"vw_small",
        @"ford_small",
        @"nissan_small"
      ] mutableCopy];

      self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

      UICollectionViewFlowLayout *layout =
          [[UICollectionViewFlowLayout alloc] init];
      layout.itemSize = CGSizeMake(106, 106);
      layout.minimumLineSpacing = 1.0;
      layout.minimumInteritemSpacing = 1.0;
      self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame
                                               collectionViewLayout:layout];
      [self.collectionView setDataSource:self];
      [self.collectionView setDelegate:self];
      self.collectionView.alwaysBounceVertical = YES;
      [self.collectionView setContentInset:UIEdgeInsetsMake(20, 10, 10, 10)];
      [self.collectionView registerClass:[IKGCollectionViewCell class]
              forCellWithReuseIdentifier:@"myCell"];
      self.collectionView.backgroundColor = [UIColor whiteColor];
      [self.view addSubview:self.collectionView];
    }

    - (void)didReceiveMemoryWarning {
      [super didReceiveMemoryWarning];
      // Dispose of any resources that can be recreated.
    }

    #pragma CollectionView & FlowLayout methods

    - (CGSize)collectionView:(UICollectionView *)collectionView
                      layout:(UICollectionViewLayout *)collectionViewLayout
      sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
      return CGSizeMake(120, 120);
    }


    - (NSInteger)collectionView:(UICollectionView *)collectionView
         numberOfItemsInSection:(NSInteger)section {
      return self.carImages.count;
    }

    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                      cellForItemAtIndexPath:(NSIndexPath *)indexPath {
      IKGCollectionViewCell *cell =
          [collectionView dequeueReusableCellWithReuseIdentifier:@"myCell"
                                                    forIndexPath:indexPath];
      long row = [indexPath row];
      UIImage *image = [UIImage imageNamed:self.carImages[row]];
      cell.imageView.userInteractionEnabled = YES;
      cell.imageView.image = image;
      NSLog(@"cell.image view %@", image);
      // [self makeCell:(IKGCollectionViewCell *)cell atIndexPath:indexPath];

      return cell;
    }
}
@end

@interface IKGCollectionViewCell : UICollectionViewCell
@property(nonatomic, strong) UIImageView *imageView;

@end


@implementation IKGCollectionViewCell
- (id)initWithFrame:(CGRect)frame {
  self = [super initWithFrame:frame];
  if (self) {
    self.imageView = [[UIImageView alloc] initWithFrame:self.bounds];
    NSLog(@"self.bounds is %@", NSStringFromCGRect(self.bounds));
  }
  return self;
}

- (void)layoutSubviews {
  [super layoutSubviews];
  self.imageView.frame = self.contentView.bounds;
}

@end
@interface MainViewController:UIViewController
@属性(强,非原子)UICollectionView*collectionView;
@结束
@接口MainViewController()<
UICollectionViewDataSource、UICollectionViewDelegate、,
UICollectionViewDelegateFlowLayout,UIgestureRecognitizerDelegate>
@属性(非原子,强)MHTransitionShowDetail*interactivePushTransition;
@性质(非原子)CGPoint lastPoint;
@属性(非原子)CGFloat startScale;
@属性(强,非原子)NSMUTABLEARRY*carImages;
@结束
@实现主视图控制器
-(instancetype)初始化{
self=[super init];
如果(自我){
}
回归自我;
}
-(无效)viewDidLoad{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
_carImages=[@[
@“雪佛兰小”,
@“迷你们”,
@“小流浪者”,
@“小聪明”,
@“小高地人”,
@“小王子”,
@“沃尔沃小”,
@“vw_small”,
@“福特小”,
@“日产小型”
]可变拷贝];
self.view=[[UIView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
UICollectionViewFlowLayout*布局=
[[UICollectionViewFlowLayout alloc]init];
layout.itemSize=CGSizeMake(106106);
layout.minimumLineSpacing=1.0;
layout.minimumInteritemSpacing=1.0;
self.collectionView=[[UICollectionView alloc]initWithFrame:self.view.frame
collectionViewLayout:布局];
[self.collectionView setDataSource:self];
[self.collectionView setDelegate:self];
self.collectionView.alwaysBounceVertical=是;
[self.collectionView setContentInset:UIEdgeInsetsMake(20,10,10,10)];
[self.collectionView注册表类:[IKGCollectionViewCell类]
forCellWithReuseIdentifier:@“myCell”];
self.collectionView.backgroundColor=[UIColor whiteColor];
[self.view addSubview:self.collectionView];
}
-(无效)未收到记忆警告{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
#pragma CollectionView和FlowLayout方法
-(CGSize)collectionView:(UICollectionView*)collectionView
布局:(UICollectionViewLayout*)collectionViewLayout
SizeFormitedIndeXPath:(NSIndexPath*)indexPath{
返回CGSizeMake(120120);
}
-(NSInteger)collectionView:(UICollectionView*)collectionView
节中的项目编号:(NSInteger)节{
返回self.carImages.count;
}
-(UICollectionViewCell*)collectionView:(UICollectionView*)collectionView
cellForItemAtIndexPath:(NSIndexPath*)indexPath{
IKGCollectionViewCell*单元格=
[collectionView dequeueReusableCellWithReuseIdentifier:@“myCell”
forIndexPath:indexPath];
长行=[indexPath行];
UIImage*image=[UIImage ImageName:self.carImages[row]];
cell.imageView.userInteractionEnabled=是;
cell.imageView.image=图像;
NSLog(@“cell.image view%@”,image);
//[self-makeCell:(IKGCollectionViewCell*)单元格路径:indexath];
返回单元;
}
}
@结束
@接口IKGCollectionViewCell:UICollectionViewCell
@属性(非原子,强)UIImageView*imageView;
@结束
@IKGCollectionViewCell的实现
-(id)initWithFrame:(CGRect)帧{
self=[super initWithFrame:frame];
如果(自我){
self.imageView=[[UIImageView alloc]initWithFrame:self.bounds];
NSLog(@“self.bounds是%@”,NSStringFromCGRect(self.bounds));
}
回归自我;
}
-(无效)布局子视图{
[超级布局子视图];
self.imageView.frame=self.contentView.bounds;
}
@结束

您没有将单元格的imageView添加为它的子视图

所以在单元格的
initWithFrame

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.imageView = [[UIImageView alloc] initWithFrame:self.bounds];
        //Add imageView as subview
        [self addSubview:self.imageView];
        NSLog(@"self.bounds is %@", NSStringFromCGRect(self.bounds));
    }
    return self;
}

您没有将单元格的imageView添加为它的子视图

所以在单元格的
initWithFrame

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.imageView = [[UIImageView alloc] initWithFrame:self.bounds];
        //Add imageView as subview
        [self addSubview:self.imageView];
        NSLog(@"self.bounds is %@", NSStringFromCGRect(self.bounds));
    }
    return self;
}