UICollectionView don't show the cell

六眼飞鱼酱① 提交于 2019-12-13 04:35:48

问题


There is UIViewController which contain UICollectionView. Also there is UICollectionViewCell which has own class & XIB (CurrentCell.h, CurrentCell.m, CurrentCell.xib).

In my UIViewController.h:

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

In my UIViewController.m:

@synthesize collection;

In 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];

In 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;

But: if I tap intuitively to the cell didSelectItemAtIndexPath method calls.

EDITED

Now everything works!

I forgot to init the cell in 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;
}

回答1:


Register the nib, not the class. You should only need to register a class if the cell is made completely in code. If you use a xib, then register that (with registerNib:forCellWithReuseIdentifier:). If you make the cell in a storyboard, then don't register anything.



来源:https://stackoverflow.com/questions/18201574/uicollectionview-dont-show-the-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!