UICollectionView 未加载

UICollectionView is not loading

这是我在 cellForItemAtIndexPath 中的代码,我想要两个 collectionView 的负载单元。这里第一个加载完美,但第二个加载正确。

} else if (collectionView == _collBanner) {

    static NSString *cellIdentifier = @"bannerCell";

    NSDictionary *dictBanner = [arrImages objectAtIndex:indexPath.row];

    BannerCollectionViewCell *cell = (BannerCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

    [collectionView registerNib:[UINib nibWithNibName:@"BannerCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:cellIdentifier];

    [cell setupBannerCell:dictBanner];

    return cell;
}
return nil;
}

我的 sizeForItemAtIndexPath 方法是...

} else if (collectionView == _collBanner) {

    return CGSizeMake(_collBanner.frame.size.width -5, _collBanner.frame.size.height -2);
} else
    return CGSizeMake(0, 0);
}

这一行必须在 viewDidLoad 中

    [_collBanner registerNib:[UINib nibWithNibName:@"BannerCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:cellIdentifier];

问题出在以下两行的顺序:

BannerCollectionViewCell *cell = (BannerCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

[collectionView registerNib:[UINib nibWithNibName:@"BannerCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:cellIdentifier];

第一个请求 BannerCollectionViewCell 类型的单元格,但是为了 collectionView 能够将其出列,BannerCollectionViewCell 必须在 [=15] 中注册=].只有第二行将单元格类型注册到 collectionView.

首先,您必须将 BannerCollectionViewCell 类型注册到 collectionView:

[collectionView registerNib:[UINib nibWithNibName:@"BannerCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:cellIdentifier];

只有这样您才能使用以下方法使单元格出列:

BannerCollectionViewCell *cell = (BannerCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

然而,只注册一次单元格就足够了,因此最好的方法是在viewDidLoad中注册单元格(将以下行从cellForItemAt移动到viewDidLoad ):

[collectionView registerNib:[UINib nibWithNibName:@"BannerCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:cellIdentifier];