我NSCollectionView是借鑑了對方我NSCollection項目。 更新:我添加了一個示例項目 GitHub Sample ProjectNSCollectionView是借鑑NSCollectionViewItems在彼此
UPDATE:這種情況已經改變有點 When the app first launches it looks like this
UPDATE 我現在的例子有這是目前在那裏自己的筆尖文件兩種觀點,與專用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;
}
UPDATE 的ItemOne和ItemTwo類都是空的類,對於每個筆尖具有NSCollectionViewItem
其又具有圖,其中標籤。該視圖是在NSCollectionViewItem
連接到NSCollectionViewItem
由視圖屬性。有除了默認的
的NSCollectionView
網格設置爲目前沒有約束如下:
佈局:網格尺寸:最大行數:0最大列:1最小產品尺寸: 寬度:250身高:150最大物品大小:寬度:250高度:150
這是用於設置整個事物的代碼,此時不將它綁定到數據源。
看來,無論什麼我更改設置,甚至改變的CollectionView類型流量不會改變任何東西,它看起來是一樣的。
我一直在接近這個作爲AutoLayout的問題,因爲最初有一些自動佈局問題,但這些都已解決。
任何幫助將不勝感激。
取得了一些進展!我從故事板中刪除了NSCollectionViewItems,並將它們放入自定義類的自己的筆尖中。我註冊了這些類,並將我的數組更改爲包含標識符。它將兩個視圖加載到兩個單獨的列中,即使我已將網格設置爲一列。只要我滾動視圖,兩個NSCollectionViewItems再次成爲一個。 – Tempus
我試過你的代碼,它的工作原理。當你從物品中移除所有的限制時會發生什麼?你是如何設置網格佈局的? – Willeke
看來我可能一直在想這一切都是錯誤的。所以我在GitHub Repo中有一個工作版本,但它適用於Flow而不是Grid,從未像我期望的那樣使Grid運行。並且能夠通過將項目大小設置爲視圖大小來創建單個列。所以這是一種工作。但是,我想看看我是否能夠看到不同的高度 – Tempus