Get selected row and column in view based nstable

对着背影说爱祢 提交于 2020-01-05 03:55:10

问题


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 ?


回答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:



来源:https://stackoverflow.com/questions/19947408/get-selected-row-and-column-in-view-based-nstable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!