带有collectionview subview 的ios uiviewcontroller不显示单元格

标签 ios uicollectionview uicollectionviewcell uicollectionviewlayout

我有一个带有 subview 的 uiviewcontroller,它是一个 uicollection View 。我的 View Controller 实现了所有的 collectionview 委托(delegate)。出于某种原因,一些单元格是黑色的(当我滚动它们时,它们似乎是随机出现的)我的代码是:

#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

Attaching a print screen

Scrolling a bit down

And print of all of the frames for the cells that are created (the seem reasonable for 4 cells in a row):

最佳答案

我解决了你的问题

-(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;
}

在创建内容 ImageView 时,不要使用单元格的框架,而是使用 self.bounds。我不确定原因。框架可以不同,那时边界将给出实际值

关于带有collectionview subview 的ios uiviewcontroller不显示单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15498506/

相关文章:

ios - NSMutableDictionary 作为数据源 : can´t remove cell

ios - '已导出 : true' has no effect in '_specialize' attribute in Xcode 12. 5

ios - 水平 Collection View 不起作用

ios - 来自 UICollectionViewCell 的 UIAlertController

ios - 带有单元格包装内容的 UICollectionView

ios - 从 UIImageView 派生的动态创建的控件缺少函数

ios - CoreData 到 CloudKit 迁移

ios - 突出显示 uicollectionviewcell 的正确方法

ios - UICollectionView 水平滚动,删除最后一项,动画不工作

ios - 在 didSelectItemAt 中选择单元格时取消选择 cellForItemAt 中的单元格