1

我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的問題,因爲最初有一些自動佈局問題,但這些都已解決。

任何幫助將不勝感激。

+0

取得了一些進展!我從故事板中刪除了NSCollectionViewItems,並將它們放入自定義類的自己的筆尖中。我註冊了這些類,並將我的數組更改爲包含標識符。它將兩個視圖加載到兩個單獨的列中,即使我已將網格設置爲一列。只要我滾動視圖,兩個NSCollectionViewItems再次成爲一個。 – Tempus

+0

我試過你的代碼,它的工作原理。當你從物品中移除所有的限制時會發生什麼?你是如何設置網格佈局的? – Willeke

+0

看來我可能一直在想這一切都是錯誤的。所以我在GitHub Repo中有一個工作版本,但它適用於Flow而不是Grid,從未像我期望的那樣使Grid運行。並且能夠通過將項目大小設置爲視圖大小來創建單個列。所以這是一種工作。但是,我想看看我是否能夠看到不同的高度 – Tempus

回答

1

的數據陣列應保持數據,而不是NSCollectionViewItem秒。在collectionView:itemForRepresentedObjectAtIndexPath:你可以撥打makeItemWithIdentifier:forIndexPath:。致電registerClass:forItemWithIdentifier:registerNib:forItemWithIdentifier:註冊您的班級或筆尖。

更多信息在NSCollectionViewcollectionView:itemForRepresentedObjectAtIndexPath:makeItemWithIdentifier:forIndexPath:的文檔中。

編輯:

有兩種方法提供NSCollectionViewItem

registerClass:forItemWithIdentifier:。當集合視圖需要一個新項目時,它會安裝這個類。 NSCollectionViewItemNSViewController的子類,NSViewController查找與班級名稱相同的筆尖。 NSCollectionViewItem是筆尖的所有者。

registerNib:forItemWithIdentifier:。當集合視圖需要一個新項目時,它會加載這個筆尖。 NSCollectionViewItem是筆尖中的頂級對象。

您將registerClass:forItemWithIdentifier:與xib混合使用registerNib:forItemWithIdentifier:。使用registerNib:forItemWithIdentifier:或修復xib。

+0

感謝Willeke的評論。我有同樣的想法。所以我改變了它,以便NSCollectionViewItems在具有專用類的單獨的nib文件中。在View Controller中,我註冊標識符,並將數組更改爲標識符數組。在'itemForRepresentedObjectAtIndex'我'makeItemWithIdentifier'和應用程序第一次加載所有的項目被分離出來,但只要我做一個滾動操作,他們就彼此結合。所以還不夠,似乎scrollview沒有增長。 – Tempus

+0

好抓!即使我得到它的工作,Xib是不正確的,我已經修復。現在我認爲它可以用於任何一種方式。由於流類型 – Tempus

+0

的附加功能,我仍將交換網格類型,我用您的建議更新了回購,並且我修復了xib文件,現在任何一個都應該可以工作。 – Tempus

0

我想通了。

,並取得了GitHub庫與工作版本Working Version of Collection View Sample Application

的第一件事。感謝Willeke對原始xib設置方式的瞭解,我能夠獲得Grid類型的工作。但最終,如果你可以使它成爲你想要的,Grow視圖是一個更好的視圖類型,因爲它支持部分,視圖之間的距離等等。所以,儘管我開始希望使用Grid類型,我將實現在我的應用程序中增長類型。

所以我使用Grow類型完成了單列視圖。

我成功的標準是:

  • ,它可以支持非統一視圖高度(每個自定義視圖可以有它自己的高度)
  • 那有一列,並且每個自定義視圖將展開如果視圖大小擴大。

到源代碼:

@interface ViewController() 
@property NSMutableArray *cellArray; 
@property (weak) IBOutlet NSCollectionView *collectionView; 

@end 

@implementation ViewController 

@synthesize cellArray; 
@synthesize collectionView; 

- (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)numberOfSectionsInCollectionView:(NSCollectionView *)collectionView { 
    return 1; 
} 

- (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]; 

    theItem.representedObject = [cellArray objectAtIndex:[indexPath item]]; 

    return theItem; 
} 

#pragma mark - NSCollectionViewDelegate - 
- (NSSize)collectionView:(NSCollectionView *)collectionView 
       layout:(NSCollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { 
    NSLog(@"%@", indexPath); 

    NSSize size = NSMakeSize(438, 150); 
    NSInteger width = 0; 
    NSInteger height = 0; 
    NSString *label = [cellArray objectAtIndex:[indexPath item]]; 

    NSRect collectionFrame = [collectionView frame]; 

    width = collectionFrame.size.width; 

    // TODO: This needs to be based on the actual value of the view instead of hardcoding a number in. 
    if ([label isEqualToString:@"Item1"]) { 
     height = 114; 
    } else if ([label isEqualToString:@"Item2"]) { 
     height = 84; 
    } 

    size = NSMakeSize(width, height); 

    return size;  
} 

@end 

有你有它。實施並不太糟糕。在NSCollectionView中顯示的每個自定義視圖都在自己的NSCollectionViewItem和.xib文件中定義,因此它們很容易修改。

脆弱的唯一部分是我計算每個視圖的高度,它只是脆弱的,因爲我在示例應用程序中執行時很懶。在實際的實現中,我將動態地從實際視圖中抓取它們,以便它們不被綁定到靜態數字。