Search code examples
cocoanstableviewcocoa-bindings

View-based NSTableView view controllers


I'm not sure if I am doing things right but this is my problem:

I have a view-based NSTableView using bindings to an arraycontroller.

I need to do some custom drawing on each row, depending the represented object as well as capture click in certain areas so for this I would need to have a controller for each row and set outlets for the sub-views in my custom cell view, but I don't understand how I can achieve this.

If I just add an object to the nib and make the connections to it, then I cannot tell which of the views is being drawn (or has been clicked).


Solution

  • I just found someone asked a similar question and the answer to it also satisfies my needs, so for anyone ending up here, this is what I did:

    • I set my NSTableCellView controller as the delegate of the NSTableView.

    • In my NSTableCellView subclass I implement the needed methods (drawRect:, mouseUp: and so forth) and call the respective methods in the controller.

    • To access the controller I get the NSTableView and then its delegate like this:

      NSTableView *tableView = (NSTableView*)myView.superview.superview.superview;
      MyControllerClass *controller = (MyControllerClass*)tableView.delegate;
      [controller view:myView drawRect:dirtyRect]
      
    • On the controller, to tell which view is sending an event, I use their identifiers.