繁体   English   中英

UICollectionView不显示单元格

[英]UICollectionView don't show the cell

UIViewController包含UICollectionView 还有UICollectionViewCell ,它具有自己的类和XIB(CurrentCell.h,CurrentCell.m,CurrentCell.xib)。

在我的UIViewController.h

@property (strong, nonatomic) IBOutlet UICollectionView *collection;

在我的UIViewController.m

@synthesize collection;

viewDidLoad中

[self.collection registerClass:[CurrentCell class] forCellWithReuseIdentifier:@"cellCurRecipe"];

UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setItemSize:CGSizeMake(120, 90)];
[flowLayout setSectionInset:UIEdgeInsetsMake(5, 10, 5, 10)];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[self.collection setCollectionViewLayout:flowLayout];

cellForItemAtIndexPath中

static NSString *CellIdentifier = @"cellCurRecipe";    
CurrentCell *cell = (CurrentCell *)[self.collection dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];   
Recipes *recipe = [currRecipesArray objectAtIndex:indexPath.item];  
[cell.image setImage: recipe.image]; 
return cell;

但是:如果我直观地点击单元格didSelectItemAtIndexPath方法调用。

已编辑

现在一切正常!

我忘了初始化CurrentCell.m中的单元格:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"CurrentCell" owner:self options:nil];

        if ([arrayOfViews count] < 1) {
            return nil;
        }

        if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]]) {
            return nil;
        }

        self = [arrayOfViews objectAtIndex:0];
    }
    return self;
}

注册笔尖,而不是课程。 如果单元格完全是用代码制作的,则只需要注册一个类。 如果您使用的是xib,则进行注册(使用registerNib:forCellWithReuseIdentifier :)。 如果将单元格放在情节提要中,则不要注册任何内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM