GoodSp33d
GoodSp33d

Reputation: 6282

Get notified for selecting a row in NSTableView

As of now I am using tableViewSelectionDidChange notification to detect when a user clicks on any row in NSTableView. But using this notification delegate I need to deselect the selected row [tableView deselectRow:[tableView selectedRow]] as it will not notify until another row gets selected.

The problem with this approach is that once I deselect a row tableViewSelectionDidChange will get notified again and now I need to check if selectedRow is -1 or not(Since no rows are selected after deselecting it will now return -1)

Is there an equivalent for tableView:didSelectRowAtIndexPath in NSTableView as in UITableView ? If not is there any better way to get notified when selecting the same row ?

Upvotes: 4

Views: 1412

Answers (1)

Tiago Almeida
Tiago Almeida

Reputation: 14237

I had the same issue and ended up here aswell.

I had a scenario where I had a playlist of songs. I wanted to have the state as "selected" in a song but be able to stop that song and play again selecting the tableview's cell.

I ended up using a solution I saw here in stackoverflow by Peter Lapisu.

Basically Peter suggests to extend the tableview create an extendedDelegate property that has the method - (void)tableView:(NSTableView *)tableView didClickedRow:(NSInteger)row;. Then just override the mouseDown event like this :

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

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

    [super mouseDown:theEvent];

    if (clickedRow != -1) {
        [self.extendedDelegate tableView:self didClickedRow:clickedRow];
    }
}

Upvotes: 2

Related Questions