【发布时间】:2017-11-19 22:07:53
【问题描述】:
我的 NSCollectionView 正在将我的 NSCollection 项目相互绘制。 更新:我添加了一个示例项目 GitHub Sample Project
更新:情况有所改变 When the app first launches it looks like this
更新
我当前的示例有两个视图,它们当前位于自己的 nib 文件中,具有专用的 NScollectionViewItem 对象,它们当前用于测试是相同的。我基本上有一个 NSCollectionViewItem 作为它的子视图,其中包含 NSTextField 。有所有的限制。
对于集合视图,它被设置为网格控制器,理想情况下,我希望有 1 列。
为了用数据加载它,我将 ViewController 设为 NSCollectionViewDataSource,并实现了 - (NSInteger)collectionView:(NSCollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 和 - (NSCollectionViewItem *)collectionView:(NSCollectionView *)collectionView
itemForRepresentedObjectAtIndexPath:(NSIndexPath *)indexPath
更新代码 包含完整代码:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[collectionView registerClass:ItemOne.class forItemWithIdentifier:@"Item1"];
[collectionView registerClass:ItemTwo.class forItemWithIdentifier:@"Item2"];
cellArray = [@[@"Item1", @"Item2", @"Item1", @"Item2", @"Item1"] mutableCopy];
}
- (void)setRepresentedObject:(id)representedObject {
[super setRepresentedObject:representedObject];
// Update the view, if already loaded.
}
#pragma mark - NSCollectionViewDatasource -
- (NSInteger)collectionView:(NSCollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section {
// We are going to fake it a little. Since there is only one section
NSLog(@"Section: %ld, count: %ld", (long)section, [cellArray count]);
return [cellArray count];
}
- (NSCollectionViewItem *)collectionView:(NSCollectionView *)collectionView
itemForRepresentedObjectAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"IndexPath: %@, Requested one: %ld", indexPath, [indexPath item]);
NSLog(@"Identifier: %@", [cellArray objectAtIndex:[indexPath item]]);
NSCollectionViewItem *theItem = [collectionView makeItemWithIdentifier:[cellArray objectAtIndex:[indexPath item]] forIndexPath:indexPath];
return theItem;
}
更新
ItemOne 和 ItemTwo 类都是空类,每个类的 nib 都有一个 NSCollectionViewItem,而后者又具有一个带有标签的视图。视图通过NSCollectionViewItem 中的视图属性连接到NSCollectionViewItem。目前除了默认的没有约束
NSCollectionView 网格设置如下:
布局:网格尺寸:最大行数:0 最大列数:1 最小项目大小: 宽度:250 高度:150 最大物品尺寸:宽度:250 高度:150
这是设置整个事物的代码,此时不将其绑定到数据源。
似乎无论我如何更改设置,甚至将 CollectionView 类型更改为 Flow 都不会改变任何内容,它看起来都一样。
我一直将此作为自动布局问题处理,因为最初存在一些自动布局问题,但这些问题都已解决。
任何帮助将不胜感激。
【问题讨论】:
-
取得了一些进展!我从情节提要中删除了 NSCollectionViewItems 并将它们放在带有自定义类的自己的笔尖中。我注册了这些类,并更改了我的数组以包含标识符。即使我将网格设置为一列,它也会将两个视图加载到两个单独的列中。当我滚动视图时,两个 NSCollectionViewItems 再次合二为一。
-
我试过你的代码,它可以工作。当您从项目中删除所有约束时会发生什么?您是如何设置网格布局的?
-
看来我可能一直在想这一切都错了。所以我在 GitHub Repo 中有一个工作版本,但它适用于 Flow 而不是 Grid,从来没有让 Grid 以我期望的方式工作。并且能够通过使项目大小与视图大小一样大来创建单个列。所以这是一种工作。但是,我想看看我是否可以看到不同高度的视图
标签: objective-c macos autolayout nscollectionview nscollectionviewitem