Two TableViews in the one Window - Datasource??

Discussion in 'Mac Programming' started by Fritzables, Jun 21, 2011.

  1. macrumors regular

    Joined:
    May 6, 2011
    Location:
    Brisbane AUSTRALIA
    #1
    Hi All,

    Well, thanks to the people who helped out on my last question, I have finally worked out how to use the NSTableView and loading it with data from different arrays.

    OK... now my window needs to have an additional TableView.

    The issue is, the Class won't allow me to have two data source's:

    Code:
    - (NSInteger)numberOfRowsInTableView:(NSTableView *)inputView {
    	return [inputArray count];
    }
    
    - (id)tableView:(NSTableView *)inputView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex {
    	return [inputArray objectAtIndex:rowIndex];
    }
    How do I now get around this?

    Pete
     
  2. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    The bad approach.

    Code:
    - (NSInteger)numberOfRowsInTableView:(NSTableView *)inputView {
         if (inputView == firstTable) {
    	return [inputArray count];
         } else if (inputView == secondTable)  {
            return [secondArray count];
         } else {
            // TODO: Throw exception
         }
    }
    
    // (etc...)
    
    The better approach is to have 2 view controllers, one for each table.
     
  3. thread starter macrumors regular

    Joined:
    May 6, 2011
    Location:
    Brisbane AUSTRALIA
    #3
    G'Day jiminaus,

    Hmmm, I was thinking that but thought it was silly..... obviously not.

    Thanks for the tip.

    Pete
     
  4. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #4
    G'day Pete (nice to be writing to a fellow Aussie BTW).

    No, it's not silly at all. It's the usual pattern. It's not that uncommon to have a window controller and then multiple view controllers. Especially if a view controller can be reused in multiple windows. Or in your case, being able to dynamically swap view controllers in and out.

    Jim
     
  5. macrumors 603

    Joined:
    Aug 9, 2009
    #5
    The data source doesn't have to be a view controller. Sometimes all that's needed is a data source for a table view, not an entire view controller for it.

    A single view controller can contain two (or more) data source objects. This would only make sense if the view being controlled had two or more table views. So simply define two or more classes that implement the required protocol, and instantiate them in the view controller.
     

Share This Page