gpt4 book ai didi

ios - 如何注册自定义 CollectionViewCell?

转载 作者:行者123 更新时间:2023-11-29 10:31:42 30 4
gpt4 key购买 nike

到目前为止,我一直在成功使用 UITableViews,但我无法让 UICollectionView 工作。

@interface NewsCollectionViewDataSource : NSObject <UICollectionViewDataSource, UICollectionViewDelegate>

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
TNCollectionViewCell *newscell = nil;
newscell = [collectionView dequeueReusableCellWithReuseIdentifier:newsCellReuseIdentifier forIndexPath:indexPath];
if (!newscell) {
[[NSBundle bundleForClass:[self class]] loadNibNamed:@"TNCollectionViewCell" owner:self options:nil];
}
newscell.newsLabel.text = [[self newsForIndexPath:indexPath] headLine];
return newscell;
}

每次我每次都排队:

newscell.newsLabel.text = [[self newsForIndexPath:indexPath] headLine];

newscell 为零。

我是不是记错了?

最佳答案

在为 collectionView 检索单元格之前的某处(即在 viewDidLoad 中)调用 UICollectionView 的以下方法

- (void)registerNib:(UINib *)nib forCellWithReuseIdentifier:(NSString *)identifier;

在你的情况下它会是这样的:

[self.collectionView registerNib:[UINib nibWithNibName:@"TNCollectionViewCell" bundle:nil] 
forCellWithReuseIdentifier:newsCellReuseIdentifier];

然后就不需要下面的代码了:

if (!newscell) {
[[NSBundle bundleForClass:[self class]] loadNibNamed:@"TNCollectionViewCell" owner:self options:nil];
}

关于ios - 如何注册自定义 CollectionViewCell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29320853/

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