Multiple UITableViewController with multiple UITablesViewCells in a single UITableView

151 Views Asked by At

I am working on a list display in an iPad app.

I have a screen with a UISegmentedController and a UITableView on the screen.

The UITableViewControllers are each defined in a class, each with their own Delegate and DataSource methods for accessing and controlling the list. There is a shared super-super-class, as well as two super-classes defined.

I previously had the lists in the their own screens, but would like to have them in a single screen and switch between the views using the Selector.

Currently I have the code set up to switch between the UITableViews and try to display the data, and have the code calling the right functions in the correct methods, but no data is appearing in the UITableView in the screen.

I'm sure that there is some "Magic" that I am not applying to the problem, but the "Spell" isn't in any of the examples I have found so far.

How do I switch between UITableViewController with their own UITableViewCells in an existing TableView?

I'm trying to do this in IOS 5.1 and using Xcode 4.6.3 as the development environment.

2

There are 2 best solutions below

9
On

Here's the code I'm using to "Swap" UITableViews:

-(void)selectView:(NSInteger)selected
{
SectionListViewController *listViewController;

switch (selected) {
    default:
    case 0:  // Animal by Name
        listViewController = [[AnimalNameListViewController alloc] init];
        break;

    case 1:  // Animal by Breeder
        listViewController = [[AnimalBreederListViewController alloc] init];
        break;

    case 2:  // Animal by Owner
        listViewController = [[AnimalOwnerListViewController alloc] init];
        break;

    case 3:  // Animal by Breed
        listViewController = [[AnimalBreedListViewController alloc] init];
        break;

    case 4:  // Animal by RegOrg
        listViewController = [[AnimalRegOrgListViewController alloc] init];
        break;

    case 5:  // Breeder by Name
        listViewController = [[BreederNameListViewController alloc] init];
        break;
}

[listViewController initalizeTableListData];

[self.listView setDataSource:listViewController];
[self.listView setDelegate:listViewController];
self.listView = listViewController.tableView;

[listViewController.tableView reloadData];
}

It "Almost" works. The Delegate and DataSource routines in the Class are called, but no data in the View.

1
On

You can use one UITableView to display all the data. Instead to using UISegmentedControl you can use UIButtons. And in IBAction of the UIButton you can change the dataArray for the particular data you want to display. Like

self.dataArray = array1;

[self.tableview reloadData];

And use this dataArray to display data in your UITableView method.

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    .....
}

OR

You can use a different approach which i used in one of my apps.

#pragma mark - UITableView Delegate and DataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

        if (btnFavourites.selected) {
            return arrFavourites.count;
        }else if (btnHistory.selected){
            return arrHistories.count;
        }else if (btnHome.selected){
            return arrAddresses.count;
        }

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        static NSString *cellIdentifier = @"MyCell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }

        // Configure the cell...
        if (btnHome.selected) {
            Home *home = [arrAddresses objectAtIndex:indexPath.row];
            cell.textLabel.text = home.address;//[address valueForKey:@"address"];

        }else if (btnFavourites.selected){
            Favourites *favourite = [arrFavourites objectAtIndex:indexPath.row];
            cell.textLabel.text = favourite.location;//[favourite valueForKey:@"address"];

        }else if (btnHistory.selected){
            History *history = [arrHistories objectAtIndex:indexPath.row];
            cell.textLabel.text = history.location;// [history valueForKey:@"address"];

        }


        return cell;

}

You can customize according to your requirement. Hope this helps. Thanks. :)