8

I've developed an app for Mac OS X Lion using its new view-based NSTableView, but as I want to port the whole app to Snow Leopard I'm trying to figure out the best way to emulate such a tableview. So far I've created a NSCollectionView and everything is fine, except for the fact that I can't get the index of the view from which a button click event is triggered. In Lion I have the following function:

- (IBAction)buttonClick:(id)sender

so I can get the index of the view inside the tableview using a method (I can't remember its name) like

- (NSInteger)rowForView:(NSView *)aView

with aView being the sender's superview, but I couldn't find something similar for the collection view ... The only "useful" method seems to be

- (NSCollectionViewItem *)itemAtIndex:(NSUInteger)index

(or something like this), but this can't help me as it returns a NSCollectionViewItem and I can't even access it knowing only the corresponding view!

Nickkk
  • 2,261
  • 1
  • 25
  • 34

5 Answers5

4

Within buttonClick, try this code:

id collectionViewItem = [sender superview];
NSInteger index = [[collectionView subviews]  indexOfObject:collectionViewItem];
return index;

Hope this helps :)

Devarshi
  • 16,440
  • 13
  • 72
  • 125
  • 1
    I can't believe it, it seems to work! I'm not sure this is the best was to achieve this (Apple should have implemented a better method) but anyway this solves my problem. Thank you so much! – Nickkk Oct 24 '11 at 21:57
  • 1
    A word of warning: this does not work anymore once the view items are reordered (e.g. if you support rearranging by drag & drop). – Mark Jun 28 '12 at 07:55
2

Geesh! Both of those approaches have issues. I can see how the first on may work, but note that the "collectionViewItem" is actually the view, NOT the collectionViewItem, which is a view controller.

The second way will not work, unless you subclass the button and put in a back link to the collectionViewItem. Otherwise, your view does not know what collectionViewItem controls it. You should use a selector binding to the collectionViewItem's representedObject instead, to get the action to the correct object in your array.

1

How about something like:

id obj = [collectonViewItem representedObject];
NSInteger index = [[collectionView contents] indexOfObject:obj];
Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
  • Like I said, I can't access the collectonViewItem only knowing the view representing it ... – Nickkk Oct 22 '11 at 17:10
0

As I suggested here: How to handle a button click from NSCollectionView

I would do it like this (because the button you want to press should be coupled with the corresponding model, therefore the represented object):

  1. Add a method to the model of your collectionViewItem (e.g. buttonClicked)
  2. Bind the Button Target to Collection View Item
  3. While binding set model key path to: representedObject
  4. While binding set selectorname to: methodname you chose earlier (e.g. buttonClicked)
  5. Add protocol to your model, if you must tell delegate or establish observer-pattern
Community
  • 1
  • 1
Dom
  • 658
  • 6
  • 10
0
  1. use NSArrayController for binding to NSCollectionView,

  2. use collectonViewItem.representedObject to get a Custom Model defined by yourself.

  3. save and get index in your custom model.

That's works for me.

kimimaro
  • 1,263
  • 2
  • 12
  • 21