具有collectionview子视图的ios uiviewcontroller不显示单元格

具有collectionview子视图的ios uiviewcontroller不显示单元格,ios,uicollectionview,uicollectionviewcell,uicollectionviewlayout,Ios,Uicollectionview,Uicollectionviewcell,Uicollectionviewlayout,我有一个带有子视图的uiviewcontroller,子视图是uicollection视图。我的视图控制器实现所有collectionview委托。由于某些原因,某些单元格是黑色的(当我滚动它们时,它们似乎是随机出现的),我的代码是: #import "NewGalleryViewController.h" @interface MyCell : UICollectionViewCell @property (nonatomic,strong) UIImageView *imageView;

我有一个带有子视图的uiviewcontroller,子视图是uicollection视图。我的视图控制器实现所有collectionview委托。由于某些原因,某些单元格是黑色的(当我滚动它们时,它们似乎是随机出现的),我的代码是:

#import "NewGalleryViewController.h"

@interface MyCell : UICollectionViewCell

@property (nonatomic,strong) UIImageView *imageView;
@end

@implementation MyCell

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    NSLog(@"frame = %@\n", NSStringFromCGRect(frame));
    if (self)
    {
        self.imageView = [[UIImageView alloc] initWithFrame:frame];
        [self.contentView addSubview:self.imageView];
    }
    return self;
}

@end

@interface NewGalleryViewController () <UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic,strong) UICollectionView *collectionView;
@end

@implementation NewGalleryViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    [layout setItemSize:CGSizeMake(75, 75)];

    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;

    [self.collectionView registerClass:[MyCell class] forCellWithReuseIdentifier:@"newcell"];
    [self.view addSubview:self.collectionView];
}


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

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 100;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"newcell" forIndexPath:indexPath];

    cell.imageView.image = [UIImage imageNamed:@"114"];
    return cell;
}



-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%ld",(long)indexPath.row);
}

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

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
@end
#导入“NewGalleryViewController.h”
@接口MyCell:UICollectionViewCell
@属性(非原子,强)UIImageView*imageView;
@结束
@实现迈塞尔
-(id)initWithFrame:(CGRect)帧
{
self=[super initWithFrame:frame];
NSLog(@“frame=%@\n”,NSStringFromCGRect(frame));
如果(自我)
{
self.imageView=[[UIImageView alloc]initWithFrame:frame];
[self.contentView addSubview:self.imageView];
}
回归自我;
}
@结束
@接口NewGalleryViewController()
@属性(非原子,强)UICollectionView*collectionView;
@结束
@NewGalleryViewController的实现
-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
如果(自我){
//自定义初始化
}
回归自我;
}
-(无效)viewDidLoad
{
[超级视图下载];
//加载视图后执行任何其他设置。
UICollectionViewFlowLayout*布局=[[UICollectionViewFlowLayout alloc]init];
[layout setItemSize:CGSizeMake(75,75)];
self.collectionView=[[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
self.collectionView.delegate=self;
self.collectionView.dataSource=self;
[self.collectionView注册表类:[MyCell类]forCellWithReuseIdentifier:@“newcell”];
[self.view addSubview:self.collectionView];
}
-(无效)未收到记忆警告
{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
-(NSInteger)collectionView中的节数:(UICollectionView*)collectionView
{
返回1;
}
-(NSInteger)collectionView:(UICollectionView*)collectionView项目编号截面:(NSInteger)截面
{
返回100;
}
-(UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath
{
MyCell*cell=[collectionView dequeueReusableCellWithReuseIdentifier:@“newcell”forIndexPath:indexPath];
cell.imageView.image=[UIImage ImageName:@“114”];
返回单元;
}
-(void)collectionView:(UICollectionView*)collectionView未取消ItemAtIndexPath:(NSIndexPath*)indexPath
{
NSLog(@“%ld”,(长)indexath.row);
}
-(CGSize)collectionView:(UICollectionView*)collectionView布局:(UICollectionViewLayout*)collectionViewLayout大小FormiteIndeXPath:(NSIndexPath*)indexPath
{
返回CGSizeMake(75,75);
}
-(UIEdgeInsets)collectionView:(UICollectionView*)collectionView布局:(UICollectionViewLayout*)collectionViewLayout插入分区索引:(NSInteger)分区
{
返回UIEdgeInsetsMake(0,0,0,0);
}
-(CGFloat)集合视图:(UICollectionView*)集合视图布局:(UICollectionViewLayout*)集合视图布局分区索引的最小线间距:(NSInteger)分区
{
返回10;
}
-(CGFloat)collectionView:(UICollectionView*)collectionView布局:(UICollectionViewLayout*)collectionViewLayout分区索引的最小元素间距:(NSInteger)分区
{
返回0;
}
@结束

我解决了你的问题

-(id)initWithFrame:(CGRect)frame
{
  self = [super initWithFrame:frame];

if (self)
{
    self.imageView = [[UIImageView alloc] initWithFrame:self.bounds]; // earlier it was frame
    [self.contentView addSubview:self.imageView];
}
  return self;
}

创建内容图像视图时,不要使用单元格的边框,而要使用
self.bounds
。我不确定原因。此时帧可以不同,边界将给出实际值

帧属性可能包含大于0的x和y位置值。bounds属性上x和y的位置值始终为0。
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        NSArray *arrayOfviews = [[NSBundle mainBundle]loadNibNamed:@"PersonCollectionViewCell" owner:self options:nil];
        self = [[arrayOfviews objectAtIndex:0]isKindOfClass:[UICollectionViewCell class]] ? [arrayOfviews objectAtIndex:0] : nil;

    }
    return self;
}