1

I have a view based NSTable with ten or more rows and five columns. To populate data for this table, I have a dictionary of 5 arrays. So each dictionary object is for each column.

Now I need to get information when I click on any cell. I am using tableViewSelectionDidChange delegate to get notified on cell selection. Using this I can get only the [myTableView selectedRow] to get to know the row selected. How can I know which column was the row in. For ex: I select/click on 5th row in 3rd column. Now I need to know which column was it clicked on so that I can extract required object from the dictionary.

Any clues ?

user88975
  • 1,618
  • 3
  • 19
  • 29

1 Answers1

1

You may override the mouseDown: event of your tableView and get the clicked column as:

- (void)mouseDown:(NSEvent *)theEvent
  {

      NSPoint globalLocation = [theEvent locationInWindow];
      NSPoint localLocation = [self convertPoint:globalLocation fromView:nil];

      NSInteger clickedCol = [self columnAtPoint:localLocation];

      [super mouseDown:theEvent];
  }

or you may use NSTableView delegate method:

 tableView:didClickTableColumn:

For reference check: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/NSTableViewDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/NSTableViewDelegate/tableView:didClickTableColumn:

Neha
  • 1,751
  • 14
  • 36
  • Just followed this [answer](http://stackoverflow.com/a/19661253/913571) and used the same logic for column. Thanks for your answer. – user88975 Nov 13 '13 at 08:48
  • 1
    `didClickTableColumn` cannot be used as its only invoked when the header of that column was selected. I couldnt find any other method other than this. – user88975 Nov 14 '13 at 04:56