UICollectionView没有显示图片,这是为什么呢?

#import "ViewController.h"
#import "Cell.h"
#import "Cell.h"
@interface ViewController () <UICollectionViewDelegate,UICollectionViewDataSource>

@end
@implementation ViewController

- (void)viewDidLoad {

CGRect  frame=    CGRectMake(0, 0, 320, 320);

    UICollectionView  *  collectionView=[[UICollectionView  alloc]initWithFrame:frame collectionViewLayout:[[UICollectionViewFlowLayout alloc]init]];
    collectionView.dataSource=self;
    collectionView.delegate=self;
    [collectionView  registerClass:[Cell class]forCellWithReuseIdentifier:@"cell"];
    [self.view  addSubview:collectionView];
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return  20;

}

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

  Cell *cell =
    [collectionView dequeueReusableCellWithReuseIdentifier:@"cell"
                                              forIndexPath:nil];
    if(cell==nil){


        NSLog(@"cell 为空");
    }
    cell.imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0,40, 40)];


    cell.imageView.image=[UIImage imageNamed:@"12"];
    cell.backgroundColor=[UIColor  redColor];

    return cell;

}
@end

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

@end

imageView不会有任何显示,这是为什么?
Cell是一个完全独立的Cell。没有依赖任何NIB文件。
就是纯粹自己写的一个类。如果是cell是从xib文件实现布局的话,使用registerNi就能显示。。。
这是为啥呢?

阅读 5.9k
1 个回答

[cell.contentView addSubview:cell.imageView];

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题