1

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).

Jeremy W. Sherman
  • 35,901
  • 5
  • 77
  • 111
Francisco Silva
  • 530
  • 1
  • 7
  • 21
  • Could you elaborate more on what you are trying to achieve. What do you mean by capture clicks? Do you have buttons or do you want to handle the events? – Suhas Sep 05 '12 at 03:44
  • I have several sub-views and I want to handle the mouseUp events. At the same time I want to draw on those sub-views with different colors depending on values from the represented object. – Francisco Silva Sep 05 '12 at 08:59

2 Answers2

1

You have to implement the delegate methods :

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row

It's used by the table view to get a view for a give cell (column, row). Then by using "makeViewWithIdentifier:owner:", you can get the a reusable cell with a given identifier and a given owner (view controller). The simplest way is to design your cells in Interface Builder, and set a different identifier for each one. Then the method "makeViewWithIdentifier:owner" will automatically create a view for you for the given identifier.

Samir
  • 627
  • 4
  • 9
-1

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.

Community
  • 1
  • 1
Francisco Silva
  • 530
  • 1
  • 7
  • 21