簡體   English   中英

帶有集合視圖子視圖的iOS uiviewcontroller不顯示單元格

[英]ios uiviewcontroller with collectionview subview not showing cells

我有一個帶有子視圖的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

附加打印屏幕

向下滾動

並為創建的單元格打印所有框架(對於連續四個單元格來說似乎是合理的):

我解決了你的問題

-(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 我不確定原因。 當時的框架可以不同,但​​會給出實際值

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM