Question

I have three UITableView's and they all need access to essentially the same data. The only difference is how each table view formats the data. What is the best way to implement this? I was thinking that if each table view had a tag of some sort, I could reference that from my data source when sending back the UITableViewCell's.

Was it helpful?

Solution

Each of the datasource calls passes in the UITableView that is requesting the information. Comparing this tableview to your three, you can determine which one it is and format the data differently. I would probably implement separate methods for each tableview and, pass the datasource method to the appropriate method. The other method would then create the customized cell creation and setup. eg for cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == [self tableView1])
        return [self tableView1:tableView cellForRowAtIndexPath:indexPath];
    if (tableView == [self tableView2])
        return [self tableView2:tableView cellForRowAtIndexPath:indexPath];
    if (tableView == [self tableView3])
        return [self tableView3:tableView cellForRowAtIndexPath:indexPath];

    Assert0(NO, @"UITableView not recognized!");
    return nil;
}

- (UITableViewCell *)tableView2:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Do normal cell creation for tableView1 here
}

I don't know if there's any gotchas off-hand for running multiple UITableViews off of the same datasource, so tread lightly.

OTHER TIPS

look into singleton design pattern - here is an example on how to implement for iOS.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top