2

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 Answers2

0

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

  • Close, but "No Banana". I have a separate class for each list that does the whole job of string things up, and two separate TableViewCell methods for the cell styles, which are both "Custom" in the storyboard. Here's the code: [listViewController initalizeTableListData]; [self.listView setDataSource:listViewController]; [self.listView setDelegate:listViewController]; self.listView = listViewController.tableView; [listViewController.tableView reloadData]; I'm getting calls to the "Delegate" and "DataSource" functions, but the display shows a blank list, no data. – user3698036 Jun 02 '14 at 23:27
0

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.

  • listViewController is an object of the SectionListViewController class and you are intializing(alloc init) it with other class. Is it even possible? I have never done any such thing. I don't know if its even possible of not. Is the initalizeTableListData method getting called? You can use debug pointer to check where and what you are doing wrong. – try catch finally Jun 03 '14 at 10:20
  • OK, Let's look at the problem from a different angle. I have a UIListViewController and a UIListViewCell that I want to display in a UIListView on a screen. What is the "Correct" method to do this w/o using the storyboard? Be patient. Most of what I know on Objective C & IOS was learned from the internet blogs. – user3698036 Jun 04 '14 at 15:23
  • UIListViewController??? Is that your class name? and is it inherited from the UITableViewController? So what you actually want to do is create a UITableView and display contents programatically. Please let me know if I m right. – try catch finally Jun 05 '14 at 04:12
  • OK. I have a super-class called SectionListViewController which is based on UITableListViewController. I have two sub-classes called AnimalListViewController (which uses AnimalTableCell) and BreederListViewController (which uses BreederTableCell). Each of the sub-classes has sub-classes. I have a UISegmentedControl in the display to select which table to display and a UITableView to display it in. So, How do I switch between "Tables"? Do I need to drop the UITableListViewController to a UITableListView? I'm getting a callus on my head from the block wall! – user3698036 Jun 05 '14 at 05:42
  • You are using .xib as the UI so you have to alloc initWithNibName: when you select the particular segment. And setFrame of the view of the controller which you want to display. Try to use this. Hope this helps. – try catch finally Jun 05 '14 at 10:42
  • You can also use debug pointers to see if the array which you are using to populate the table is empty or not. – try catch finally Jun 05 '14 at 10:43
  • No .xib set up for the Tables, but I "Think" I see where your going. I'd need to set up a .xib for the two subclasses (Animal & Breeder), then use it to populate the UITableView (listView in my sample code). – user3698036 Jun 05 '14 at 15:15
  • Using a .xib for a few Popovers, but not sure how to attach it to the UITableListView. Can you show some code? The "Frame" for the List .xibs wouldn't need to be the same size as the listView, correct? It would just have to have be "Custom" and have the right CellType to comment to the existing code. – user3698036 Jun 05 '14 at 17:05
  • You can create controller classes with .xib. And then you can design your view manually by drag dropping UITableViews. In your main view you can intialize and set frame of the controller.view to display it. – try catch finally Jun 06 '14 at 12:11