views:

238

answers:

3

In my setup I have an NSCollectionView of volumes connected to the mac. For the NSCollectionViewItem's View I have an NSBox with an NSTableView inside listing the contents of that volume.

My problem comes when trying to add/remove items to the NSTableView. The NSBox seems to be initialised once, so there is only one NSTableView. This means that when I want to update the data inside the NSTableView I cannot call reloadData on an IBOutlet and have it update all the tables.

If I create an IBOutlet in a subclass of the NSBox, it is nil for the instance, so I cannot call it via that.

The closest I have come is by enclosing a @try @catch around the code that returns the object at a row in a column, an exception occurs because that item no longer exists, so I can grab the tableView and call reloadData, which seems to update that specific NSTableView.

The problem with this is that if the item removed is at the end of the table, or if an item is added, the exception won't occur as it can see all the existing items.

Has anyone had any experience with an NSTableView on an NSCollectionViewItem's View? How did you update the tables?

+1  A: 

Put the NSBox (with its NSTableView) into its own nib. Each time you create a new collection view item, load the nib, set that NSBox as the collection view item's view, and release the NSBox.

As for feeding the table views, the easiest way is probably Bindings. Bind each column of the table view to a different property of the model object that the collection view item represents. You'll probably want to go through an array controller, of course, which means having one of those per collection view item, and adding them to an array, which you'll release in dealloc.

Peter Hosey
I've had a go at this, but have run across two problems. Firstly the code I had to bind the represented object no longer works as the IBOutlet I have bound to the new subclassed CollectionViewItem returns nothing. Secondly, the NSBox is being squashed so that all of it fits inside the NSCollectionView whereas before it was spread across two columns and if there were enough items it would scroll instead of squashing them.
Septih
Update: I've fixed the squashing issue now. Thanks to subclassing, minItemSize and maxItemSize.
Septih
OK the outlet coming back nil was due to the old view that I dragged into the new nib still being loaded (duh!) and that wasn't connected, so that caused the error. All sorted now :)
Septih
+1  A: 

I wrote a blog post about NSCollectionView which you may find useful.

Benedict Cohen
Thank you for the guide. Not sure if things have changed much since you wrote it, but I found that -LoadFromNib didn't exist and used -loadNibNamed instead.
Septih
A: 

Could you post an Xcode project illustrating how you solved your problem? Thanks!