objective-c - 如何在不对其进行子类化的情况下使用 NSCollectionViewItem?

标签 objective-c macos nscollectionview nscollectionviewitem

我正在编写一个非常简单的 macOS 应用程序,我想在 Collection View 中显示一些图像。对于它们的显示方式,我不需要任何特殊行为。 docs for NSCollectionViewItem说:

The default implementation of this class supports the creation of a simple item that displays a single image or string.

这就是我想要的。但是,我找不到有关如何创建默认 NSCollectionViewItem 的任何信息。 .

documentation for NSCollectionView状态:

Every data source object is required to implement the following methods:

collectionView:numberOfItemsInSection:

collectionView:itemForRepresentedObjectAtIndexPath:

上面的第二种方法返回一个 NSCollectionViewItem .通过阅读示例,我了解到创建 NSCollectionViewItem 的传统方式在这种情况下是调用:

NSCollectionViewItem*   newCollectionViewItem   = [imageCollectionView makeItemWithIdentifier:<some identifier>
                                                                                     forIndexPath:indexPath];

问题是我不明白什么是<some identifier>应该。我没有包含 NSCollectionViewItem 的 Nib 因为我没有以任何方式对其进行子类化或自定义。我已尝试将以下内容添加到我的数据源中:

- (void)awakeFromNib
{
    [imageCollectionView registerClass:[NSCollectionViewItem class]
                 forItemWithIdentifier:@"Image"];
}

哪里imageCollectionViewNSCollectionView有问题。然后在我的 - (NSCollectionViewItem *)collectionView:itemForRepresentedObjectAtIndexPath:方法,我调用:

NSCollectionViewItem*   newCollectionViewItem   = [imageCollectionView makeItemWithIdentifier:@"Image"
                                                                                     forIndexPath:indexPath];

但这会抛出异常并将其打印到控制台:

2016-12-19 17:51:27.463 MyApp[28177:3926764] -[NSNib _initWithNibNamed:bundle:options:] could not load the nibName: NSCollectionViewItem in bundle (null).

后跟堆栈跟踪。

那么我该如何着手创建和使用 NSCollectionViewItem没有以任何方式进行子类化或修改?

最佳答案

这是一个非常简单的示例,它使用 Nib 作为项目的原型(prototype):

@interface ViewController()

@property (weak) IBOutlet NSCollectionView *collectionView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSNib *theNib = [[NSNib alloc] initWithNibNamed:@"Item" bundle:nil];

    [self.collectionView registerNib:theNib forItemWithIdentifier:@"item"];
}

#pragma mark NSCollectionViewDataSource

- (NSInteger)numberOfSectionsInCollectionView:(NSCollectionView *)inCollectionView {
    return 1;
}

- (NSInteger)collectionView:(NSCollectionView *)inCollectionView numberOfItemsInSection:(NSInteger)inSection {
    return 10;
}

- (NSCollectionViewItem *)collectionView:(NSCollectionView *)inCollectionView itemForRepresentedObjectAtIndexPath:(NSIndexPath *)inIndexPath {
    NSCollectionViewItem *theItem = [inCollectionView makeItemWithIdentifier:@"item" forIndexPath:inIndexPath];
    NSTextField *theLabel = (NSTextField *)theItem.view;

    theLabel.stringValue = [NSString stringWithFormat:@"%d.%d", (int)inIndexPath.section, (int)inIndexPath.item];
    return theItem;
}

@end

NIB 仅包含一个 NSCollectionViewItem,其中包含一个文本字段作为 View 。

附录: 我认为您应该为 registerClass 变体创建一个 NSCollectionViewItem.xib。如果您没有在 loadView 中手动创建 View , View Controller 将使用其类名搜索 NIB。因此,您不能使用没有 NIB 的普通 NSCollectionViewItem 来注册类,因为 makeItemWithIdentifier:forIndexPath: 将访问项目的 View 。

关于objective-c - 如何在不对其进行子类化的情况下使用 NSCollectionViewItem?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41233802/

相关文章:

objective-c - Objective-C 新手,在 Xcode 包中找不到 Foundation 工具

swift - NSCollectionView 在创建项目时崩溃

objective-c - Objective-C 头文件中的常量是如何初始化的?

objective-c - 如何在 objective-c 中反转 NSInteger 或 NSUInteger 的字节顺序

mysql - 连接到 MySQL 数据库并在 Julia 中获取数据

python - 在 Mac OS 10.7 上使用 pygame 的 IO python 多处理模块

macos - 如何实现具有自动调整边距的居中项目的 NSCollectionView ?

objective-c - NSCollectionView 单元格顺序随 View 更改而更改

ios - 无法在 AFNetworking 中获取下载进度

ios - 为什么我在调用 UICollectionViewCell dequeueReusableCellWithReuseIdentifier 时会收到释放的内存调用?