2

I'm trying to display 256 cells in a NSCollectionView. I'm using Storyboard but the NSCollectionViewItem is in a separate xib. It is registered in the CollectionView at startup. A DataSource object holds an array (NSMutableArray) with numbers which are loaded into the representedObject of a NSCollectionViewItem when one is provided by the makeItemWithIdentifier(...) function.

func collectionView(collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
    return dataArray.count
}

func collectionView(collectionView: NSCollectionView, itemForRepresentedObjectAtIndexPath indexPath: NSIndexPath) -> NSCollectionViewItem {
    let item = collectionView.makeItemWithIdentifier("CellItem", forIndexPath: indexPath) as! CellCollectionViewItem
    item.representedObject = dataArray[indexPath.indexAtPosition(1)]
    item.index = indexPath.indexAtPosition(1)
    item.dataSource = self
    return item
}

When the cells should be displayed only (exactly) 100 cells are rendered instead of all 256. At no point does the data array hold 100 items. Furthermore, my debug output shows that actually all NSCollectionViewItems are produced. That means the problem must lie in the rendering of the collection view. Is there a restriction to the amount of items the NSCollectionView can render at once? What am I missing?

The remaining cells appear when the window is slowly resized by the user and disappear again if the user resizes the window fast. Does the view stop rendering after a fixed amount of time?

Cells appearing on window resize and disappearing again after a click on a cell (which triggers a reload of the reloadData() function

  • It's not a real solution to the problem but I started a new project to try and recreate the whole project step by step. The problem doesn't occur so I have no idea what I did wrong the first time. – Artem Khatchatourov Apr 27 '16 at 13:39
  • Did you every find a solution to this problem? I kind of have a similar issue, my collection view has 3 items, when I change the view and then go back to the view that contains my collection view, the order of the items randomly change. Very weird behaviour. – Supertecnoboff Oct 11 '17 at 14:11
  • 1
    @Supertecnoboff To be honest I never found a solution. The amount of magic that I did not understand was too overwhelming. I began from scratch and tried to recreate this behaviour but then it was working fine. Are you sure you are using the same instance of the matrix? Did you try to output every element that is added to the collectionView with the index it is added? – Artem Khatchatourov Oct 14 '17 at 08:03
  • I see. Yeah I ended up using an NSTableView with multiple columns. It looks exactly the same as a collection view and it’s smoother too. – Supertecnoboff Oct 14 '17 at 08:24
  • @Supertecnoboff, did you find the solution? – jimwan Jan 22 '19 at 03:33
  • @jimwan https://stackoverflow.com/a/46713935/1598906 – Supertecnoboff Jan 22 '19 at 08:41

0 Answers0