gpt4 book ai didi

ios - 以 UIView 作为委托(delegate)和数据源的 UICollectionView

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:39:31 25 4
gpt4 key购买 nike

我一定是个菜鸟,但我想不通,需要一些帮助。

我有一个 UIView,里面有一个 UICollectionView。当我添加 Collection View 时,出于某种原因,我的 Nib 中没有得到 UICollectionViewCell 我只有在 UIViewController 中添加 Collection View 时才会得到它。我的部分问题是 - 是否可以在我的 nib 文件中包含 UICollectionViewCell

我假设答案是否定的,所以我尝试了以下方法:

我为我的自定义 UICollectionViewCell 创建了一个单独的 nib 文件和类,并将自定义单元格注册到我的 Collection View 中,如下所示:

[self.itemsCollection registerClass:[CustomCollectionViewCell class] forCellWithReuseIdentifier:@"ItemCell"];

然后在我的 cellForItemAtIndexPath 方法中,我这样做:

CustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ItemCell" forIndexPath:indexPath];
cell.itemImage.image = [UIImage imageNamed:@"Some Image.png"];
cell.itemLabel.text = @"Some Title";

然而,当我的单元格被创建时,itemImageitemLabel 都是 nil

如有任何想法,我们将不胜感激!

谢谢。

最佳答案

如果您在 NIB 中定义了 IBOutlet 引用,那么您必须注册 NIB,而不是类。所以,替换:

[self.itemsCollection registerClass:[CustomCollectionViewCell class] forCellWithReuseIdentifier:@"ItemCell"];

[self.itemsCollection registerNib:[UINib nibWithNibName:@"YourNibName" bundle:nil] forCellWithReuseIdentifier:@"ItemCell"];

您的 NIB 可以指定 CustomCollectionViewCell 的基类。

关于ios - 以 UIView 作为委托(delegate)和数据源的 UICollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19474409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com