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

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就能显示。。。
这是为啥呢?


[cell.contentView addSubview:cell.imageView];

【热门文章】
【热门文章】