OGeek|极客世界-中国程序员成长平台

标题: ios - 如何在 Storyboard 中使用自定义 UICollectionViewCell? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 18:27
标题: ios - 如何在 Storyboard 中使用自定义 UICollectionViewCell?

我不明白为什么自定义单元格在 Storyboard 中无法识别。

TNTopStoriesCollectionViewController:

static NSString * const reuseIdentifier = @"TNTopNewsCellItem";

- (UICollectionViewCell *)collectionViewUICollectionView *)collectionView cellForItemAtIndexPathNSIndexPath *)indexPath
{
    TNTopStoriesCollectionViewCell *cell = (TNTopStoriesCollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    // Configure the cell
    [self preLoadImagesForCategory:_dataStore.fiveTopStories[indexPath.item] andImageView:cell.itemImage];
    return cell;
}

我正在为 collectionView 使用 Storyboard : enter image description here

并且单元格也正确指向自定义类: enter image description here

#import <UIKit/UIKit.h>

@interface TNTopStoriesCollectionViewCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet UIImageView *itemImage;
@property (weak, nonatomic) IBOutlet UILabel *title;

@end

并且还使用了正确的单元格标识符: enter image description here

但我在 cellForItemAtIndexPath 方法中得到了这个异常:

[UICollectionViewCell itemImage]: unrecognized selector sent to instance

但是为什么?

更新:

我按照建议做了所有事情,现在得到了这个: enter image description here

但是它仍然在同一个地方失败。



Best Answer-推荐答案


在场景中对象的分层名称列表中,该单元在左侧被称为“ Collection View 单元”,并且没有更改为“热门故事 Collection View 单元”这一事实表明您没有实际上已成功将 Storyboard中的类更改为 TNTopStoriesCollectionViewCell。

(请注意 Collection View Controller 如何在左侧列为热门故事 Collection View Controller 。同样的事情应该发生在单元格上。)

你在左边看到的应该更像这样:

enter image description here

(另外,你的标 checkout 口在左边被称为新闻标签这一事实暗示了一个问题,因为在代码中这个标签被称为“标题”,如果你已经上钩了正确,我希望 Xcode 在左侧列表中也将其名称更改为“标题”。)

[我有一种模糊的感觉,您可能实际上有两个类,TNTopStoriesCollectionViewCell 和 TNTopNewsCellItem,而您对它们感到困惑...]

关于ios - 如何在 Storyboard 中使用自定义 UICollectionViewCell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35275104/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4