4

Right now I am working on a Startup Window for my document based application. The window's purpose is to give the user something visual to start with, instead of the menu bar.

The window contains a NSCollectionView whose purpose is to show recent files. I did manage to get the recent documents in the collection view using a separate .xib file as a reference NSCollectionViewItem. My collection view has the Flow layout vertical.

However, the items don't look the way I want them too. In my storyboard I did select the checkbox 'Use Alternating Colors', a property for the collection view. For testing purposes I selected Red as primary color and Yellow as Secondary color. In the storyboard I immediately see the complete collection view turning Red. When I run the application my startup window shows the collection view containing all the recent documents, but all backgrounds are the primary color, which would be Red.

I did look through some tuts, also the Ray Wenderlich one, but I can't seem to find any information about the alternating colors.

This is how I create my collection view items:

- (NSCollectionViewItem *)collectionView:(NSCollectionView *)collectionView itemForRepresentedObjectAtIndexPath:(NSIndexPath *)indexPath
{   
    RecentFileCollectionViewItem *item = [collectionView makeItemWithIdentifier:@"RecentFileCollectionViewItem" forIndexPath:indexPath];
    NSURL *url = [self.recentFiles objectAtIndex:indexPath.item];

    [item.logoImageView setImage:[NSImage imageNamed:@"NSComputer"]];
    [item.fileNameLabel setStringValue:[url pathComponents][[url pathComponents].count -1]];
    [item.filePathLabel setStringValue:[url path]];

    return item;
}

Did anyone came across this problem? Is there a solution? Thanks a lot !

NRaf
  • 7,407
  • 13
  • 52
  • 91
Tom Vos
  • 410
  • 1
  • 5
  • 13
  • The way I did for selected item is to set backgroundColor using CALayer. You may set it in the didSelectItemsAtIndexPaths delegate callback. – Harry Ng Oct 18 '16 at 13:47
  • I had the same problem. The alternating checkmark doesn't seem to work for the secondary color. The primary color is accepted as the collectionView background color. IB and code didn't work. My solution was to build a custom NSCollectionViewItem and there set background color of the self.view.layer for the indexpath of self. Take note that you have to check if the item is selected because it should use the self.selectionColor (which is system defined). My solution is in swift. If you need it I can copy/paste it. – Darkwonder Mar 29 '19 at 13:43

0 Answers0