1

I have a problem with getting the clickedRow set through IB.

Setup

In IB I have a view with custom class set, call it MyView. The file's owner of the XIB is MyVC.xib. In IB I have also created an NSTableView inside MyView.

  • Thus the view hierarchy is like so MyView -> (auto-generated) NSScrollView -> NSClipView -> NSTableView.

  • The tableview is setup through bindings and connected the double-click action/target correctly, as mentioned in this post Double click an NSTableView row in Cocoa?

  • I have declared and connected an IBOutlet NSTableView in MyVC.h.

Problem

When the selector is called, the NSArray of objects are passed in nicely. However, I cannot know which of them to operate in since [_tableView clickedRow] always returns -1.

Community
  • 1
  • 1
H.Rabiee
  • 4,747
  • 3
  • 23
  • 35
  • Rather than using `clickedRow`, use the delegate method `tableView:shouldSelectRow:` to catch the selected row early. I think the problem you are running into is that the selector is called before the row selection is completed. – Daniel Farrell Jan 02 '15 at 21:33
  • Thanks for your comment. I would require a double-click action though. I am not sure what's going on. I reconnected the delegate and outlet. Now, the selector parameter with objects contains the actual clicked object `mySelector:(NSArray)objects:` instead of the whole array IF I click on a non-existent row first in the tableView and THEN click a row. Otherwise if the first thing I do is to click a row, the objects parameter contains the entire array of objects. – H.Rabiee Jan 04 '15 at 12:32
  • Do you have an example project? This is not too hard, I think it's just a matter of using the correct delegate or if you really have to, subclassing the table view. – Daniel Farrell Jan 04 '15 at 15:59
  • Unfortunately not atm. I agree this shouldn't be too hard. Btw the `tableView:shouldSelectRow:` will not suffice, since I would require different behaviour for single and double-click. Given the bindings setup, the Double-Click action should be used IMO. What do you mean by using correct delegate? `-(void)tableViewSelectionDidChange:(NSNotification *)notification` e.g is called upon startup. – H.Rabiee Jan 04 '15 at 17:51
  • I would use `-tableView:shouldSelectRow:` and use it to store the index of the selected row in your delegate. Then when you get the double click message sent, use your own invar for the row index rather than the one that always seems to be returning -1. That's was my original suggestion. Might be worth a try. Very happy to play around with an example project if you make one. It's hard to know exactly what the correct approach is but with some targeted tinkering it should be doable. Sorry I can't be of more help! The other option is to subclass, override `-mouseDown:` and... – Daniel Farrell Jan 04 '15 at 18:00
  • ... look for the double click your self by inspecting the `NSEvent` object which is passed into that method. In a similar way you can try and store the row index in your own instance variable. Only subclass as a last resort, `NSTableView` is a beautiful example of delegate pattern! – Daniel Farrell Jan 04 '15 at 18:02
  • Do you really care about the argument binding? Do you really make use of the argument to the double-click action method? Have you tried not using bindings for setting up the double-click target, action, and arguments but just setting the table view's `target` and `doubleAction` properties at NIB load time? – Ken Thomases Jan 07 '15 at 19:56
  • Yes it works without bindings, i.e just setting `target` and `doubleAction`. What's necessary is the clickedRow really. I wanted to do it in IB to keep it consistent. Setting the selector through code seems like a hack. – H.Rabiee Jan 08 '15 at 21:28

0 Answers0