TableView Delegate
The last time we created a simple table view (source). Today we will add a delegate in order to be informed about selection changes in our table. Delegates are a common way to inform another object about changes or perform specific tasks and react on the returned value.
To add a delegate to our table view all we have to do is add a single line to our existing code:
var tableView = [[CPTableView alloc] initWithFrame:[scrollView bounds]]; [tableView setDataSource:self]; [tableView setDelegate:self]; // <<--- add this line var column = [[CPTableColumn alloc] initWithIdentifier:@"Stuff"];
There are plenty methods our delegate can (but must not) react to
- (BOOL)selectionShouldChangeInTableView:(CPTableView)aTableView - (CPView)tableView:(CPTableView)tableView dataViewForTableColumn:(CPTableColumn)tableColumn row:(int)row - (void)tableView:(CPTableView)tableView didClickTableColumn:(CPTableColumn)tableColumn - (void)tableView:(CPTableView)tableView didDragTableColumn:(CPTableColumn)tableColumn - (float)tableView:(CPTableView)tableView heightOfRow:(int)row - (BOOL)tableView:(CPTableView)tableView isGroupRow:(int)row - (void)tableView:(CPTableView)tableView mouseDownInHeaderOfTableColumn:(CPTableColumn)tableColumn - (int)tableView:(CPTableView)tableView nextTypeSelectMatchFromRow:(int)startRow toRow:(int)endRow forString:(CPString)searchString - (CPIndexSet)tableView:(CPTableView)tableView selectionIndexesForProposedSelection:(CPIndexSet)proposedSelectionIndexes - (BOOL)tableView:(CPTableView)aTableView shouldEditTableColumn:(CPTableColumn)aTableColumn row:(int)rowIndex - (BOOL)tableView:(CPTableView)aTableView shouldSelectRow:(int)rowIndex - (BOOL)tableView:(CPTableView)aTableView shouldSelectTableColumn:(CPTableColumn)aTableColumn - (BOOL)tableView:(CPTableView)tableView shouldShowCellExpansionForTableColumn:(CPTableColumn)tableColumn row:(int)row - (BOOL)tableView:(CPTableView)tableView shouldTrackView:(CPView)view forTableColumn:(CPTableColumn)tableColumn row:(int)row - (BOOL)tableView:(CPTableView)tableView shouldTypeSelectForEvent:(CPEvent)event withCurrentSearchString:(CPString)searchString - (CPString)tableView:(CPTableView)aTableView toolTipForView:(CPView)aView rect:(CPRectPointer)rect tableColumn:(CPTableColumn)aTableColumn row:(int)row mouseLocation:(CPPoint)mouseLocation - (CPString)tableView:(CPTableView)tableView typeSelectStringForTableColumn:(CPTableColumn)tableColumn row:(int)row - (void)tableView:(CPTableView)aTableView willDisplayView:(id)aView forTableColumn:(CPTableColumn)aTableColumn row:(int)rowIndex - (void)tableViewSelectionDidChange:(CPNotification)aNotification - (void)tableViewSelectionIsChanging:(CPNotification)aNotification
Most of them are pretty obvious when they are called. For detailed information you can visit the apple documentation for the NSTableViewDelegate Protocol.
As an example we now can simply implement any of these delegate methods in our AppController
- (void)tableViewSelectionDidChange:(CPNotification)aNotification
{
row = [[[aNotification object] selectedRowIndexes] firstIndex];
console.info(row);
}
after reloading our index.html you should see the index of the current selected row (or -1 if nothing is selected) in your browsers console

Now you can do anything you like in order to react on selection changes. (Code on github).
