user88975
user88975

Reputation: 1648

Get selected row and column in view based nstable

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 ?

Upvotes: 1

Views: 1164

Answers (1)

Neha
Neha

Reputation: 1751

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:

Upvotes: 1

Related Questions