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

Posted

技术标签:

【中文标题】Imageview 未显示在以编程方式创建的 collectionview 中【英文标题】:Imageview is not showing up in a programmatically created collectionview 【发布时间】:2016-04-29 05:04:19 【问题描述】:

我很难弄清楚为什么cell.imageview 属性没有在下面显示imageUIImage 不为零,单元格已创建,如果我为单元格分配背景,则会显示它,这证明我正在正确创建 collectionview 和我的单元格。我以编程方式创建我的collectionview,并且不使用任何 IB 或interface builder。我以前做过很多次,但我真的对这个摸不着头脑。

提前致谢。

    @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

【问题讨论】:

【参考方案1】:

您错过了将单元格的 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 未显示在以编程方式创建的 collectionview 中的主要内容,如果未能解决你的问题,请参考以下文章

无法在以编程方式创建的窗口中单击 iAd - 未调用bannerViewActionShouldBegin?

允许在以模式方式显示的以编程方式创建的视图内自动旋转

以编程方式添加 imageview 约束问题

如何在以编程方式创建的 UIButton 上添加 padding-left?

iOS:以编程方式在以编程方式创建的滚动视图中创建标签

UIPickerView 委托在以编程方式创建时从不调用