Reputation: 4797
I'm trying to display an NSPopover
with additional information to a selected row of an NSTableView
. For that I need to get a reference to the view representation of the selected row so I can "attach" my popover to it:
NSInteger row = [[self membersTableView] selectedRow];
NSTableRowView *aView = [[self membersTableView] rowViewAtRow: row makeIfNecessary: YES];
[self setQuickLookPopoverController: [QuickLookPopoverController showPopoverFor: anObject at: aView]];
In the above, the result of aView
is always nil. According to Apple documentation, this is the method to obtain a view object, given a selected row. Especially the last sentence of the discussion is a bit weird:
Discussion This method will first attempt to return a currently displayed view in the visible area. If there is no visible view, and makeIfNecessary is YES, a prepared temporary view is returned. If makeIfNecessary is NO, and the view is not visible, nil will be returned.
In general, makeIfNecessary should be YES if you require a resulting view, and NO if you only want to update properties on a view only if it is available (generally this means it is visible).
An exception will be thrown if row is not within the numberOfRows. The returned result should generally not be held onto for longer than the current run loop cycle. It is better to call rowViewAtRow:makeIfNecessary: whenever a view is required..
Why is this method always returning nil?
Upvotes: 1
Views: 1069
Reputation: 936
Thanks for showing me the right direction, I had the same problem! I ended up doing the following, but note that I disabled selection of empty rows and that the following code is inside an IBAction
:
[popOver showRelativeToRect:[sender bounds]
ofView:[sender rowViewAtRow:[sender selectedRow]
makeIfNecessary:YES]
preferredEdge:NSMaxXEdge];
Upvotes: 0
Reputation: 4797
Solved it. I used NSTableView's method (NSRect) rectOfRow: (NSInteger) rowIndex
which will give the frame of the required row.
Upvotes: 1