0

I have 3 UITableViews in a class; one of the tableViews is programmatically created in a UIPopover where I assign it a tag. In -cellForRowAtIndexPath, I check the tag each tableView and configure the tableView depending on the tag id.

The problem is the popover is not created until after -cellForRowAtIndexPath is called. I don't see how I can have a separate -cellForRowAtIndexPath in the method that creates the tableView in the popover. I have the tableView.dataSource = self in the method that creates the tableView.

How can I point the tableView in the popover to it's own -cellForRowAtIndexPath?

SpokaneDude
  • 4,856
  • 13
  • 64
  • 120
  • 1
    A given TableView will invoke the `cellForRowAtIndexPath` contained in the class identified by the `dataSource` pointer (*not* the `delegate` pointer). There can be no other, and `cellForRowAtIndexPath` cannot be called until the `dataSource` pointer is set (and the TableView appropriately constructed, etc). – Hot Licks Jan 09 '15 at 01:04
  • It was a typo on my part... I meant dataSource... (I know the difference). Is that why the question was down voted? – SpokaneDude Jan 09 '15 at 04:26
  • You apparently don't understand what the `dataSource` is. You can't have multiple versions of cellForRowAtIndexPath without multiple dataSources. Your question is nonsensical. – Hot Licks Jan 09 '15 at 13:03

1 Answers1

2

So it wasn't entirely clear, but it seems as though your asking how to assign different cellForRowAtIndexPath for the different TableView's in your class. To this end, I've created this small piece of sample code to illustrate how you could accomplish having differing sets of data sources for multiple UITableView's in a single class.

As you can see, there are three different DataSource objects that can each independently control the cellForRowAtIndexPath for each of the three different UITableView's. There's no reason you can't have two table views utilize a single DataSource, and then the third table use it's own.

*Note: There is no reason to keep all of this in a single file, but if that is your desire you certainly can do that.

//UITableViewOne
@interface DataSourceOne : NSObject <UITableViewDataSource>
@end

@implementation DataSourceOne
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  {
    // Setup cell for TableViewOne
}
@end

//UITableViewTwo
@interface DataSourceTwo : NSObject <UITableViewDataSource>
@end

@implementation DataSourceTwo
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  {
    // Setup cell for TableViewTwo
}
@end

//UITableViewThree
@interface DataSourceThree : NSObject <UITableViewDataSource>
@end

@implementation DataSourceThree
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  {
    // Setup cell for TableViewThree
}
@end

@interface MultiTableViewController ()

@property (nonatomic,strong) UITableView *tableOne;
@property (nonatomic,strong) UITableView *tableTwo;
@property (nonatomic,strong) UITableView *tableThree;

@property (nonatomic,strong) DataSourceOne *sourceOne;
@property (nonatomic,strong) DataSourceTwo *sourceTwo;
@property (nonatomic,strong) DataSourceThree *sourceThree;

@end

@implementation MultiTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.sourceOne = [DataSourceOne new];
    self.sourceTwo = [DataSourceTwo new];
    self.sourceThree = [DataSourceThree new];

    //Create or Load TableViews from Xib

    self.tableOne.dataSource    = self.sourceOne;
    self.tableTwo.dataSource    = self.sourceTwo;
    self.tableThree.dataSource  = self.sourceThree;

}

Let me know if you want any more clarification, or if you have further questions on the topic.

SethHB
  • 743
  • 4
  • 12
  • Wow! I wasn't aware of how to do it... perfectly clear and I'll get on it later on tonight after dinner... thank you so much! – SpokaneDude Jan 09 '15 at 00:02