问题
I have a NSCollectionView
and I am adding my custom view which acts as NSCollectionViewItem
for that collection view. In my collectionViewItem, I have a NSButton
along with various other elements. I have a method onButtonClick
which is connected to that button. Now, suppose I add 5 items of my collectonViewItem on to the collectionView.
How can I get the index of the view from where the button was clicked?
Inside onButtonClick
, I tried following code but it always returns 0 regardless of which button I click:
id collectionViewItem = [sender superView];
NSInteger index = [[colloectionView subviews] indexOfObject:collectionViewItem];
What is the right way to achieve this?
回答1:
you cant return index by clicking a control (which having its own functon).i think you have to see http://andrehoffmann.wordpress.com/2009/08/29/nscollectionview-tutorial-for-dummies-xcode-3-1-3/. before that check collectionview selection as selectable in attributes inspector.(and also try by disable the button)
来源:https://stackoverflow.com/questions/12814028/get-the-index-of-view-from-which-a-button-was-clicked-in-a-nscollectionview