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

Posted

技术标签:

【中文标题】带有collectionview子视图的ios uiviewcontroller不显示单元格【英文标题】:ios uiviewcontroller with collectionview subview not showing cells 【发布时间】:2013-03-19 11:40:59 【问题描述】:

我有一个带有子视图的 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

【问题讨论】:

【参考方案1】:

我解决了你的问题

-(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。我不确定原因。那时的帧可以不同,界限会给出实际值

【讨论】:

frame 属性可能包含大于 0 的 x 和 y 位置值。bounds 属性上 x 和 y 的位置值将始终为 0。【参考方案2】:
- (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;

【讨论】:

以上是关于带有collectionview子视图的ios uiviewcontroller不显示单元格的主要内容,如果未能解决你的问题,请参考以下文章

如何将collectionview作为子视图添加到viewcontroller

带有集合视图的动态布局 iOS

iOS - 带有表格视图和子视图的选项卡式应用程序

iOS:表视图(带有 FetchedResultsController)作为另一个视图的子视图

UICollectionView的选择性手势识别,UIView为子视图

带有按钮的 iOS 子视图不响应触摸。