Kutay Demireren
Kutay Demireren

Reputation: 650

Using UISegmentedControl to Change the Data of Table View

In my app, there will be actually two table view but they will not really different from each other. The only difference between two view is the URL they have, because I update my table with the data coming from the URL. Everything other than URL remains same. I used to have 2 different view controller and switch between them, but later I thought having one Table View Controller and just change the URL and update the table with the given URL is a better idea.

I came up with this:

enter image description here

And below there is a part of my viewDidLoad function in table, where I take the data from URL (I'm using AFNetworking):

enter image description here

Problem I'm having here is, it doesn't reload the data; although I use reloadData method of the table. Shortly, I switch but nothing happens.

What am I missing do you think? Or the way I thought is wrong from the beginning?

For convenience, here is the storyboard I have, it is simple :)

enter image description here

Upvotes: 0

Views: 430

Answers (2)

damian
damian

Reputation: 3674

You could try using the UITableViewDataSource protocol. By default a UITableViewController's dataSource is set to self (ie the UITableViewController is the data source, which is why it has all those tableView:cellForRowAtIndexPath: methods).

What you could do instead is move all those methods to your own class, MyDataSource, then on init:

self.dovizDataSource = [[MyDataSource alloc] initWithURL:<dovizURL>];
self.altinDataSource = [[MyDataSource alloc] initWithURL:<altinURL>];

When you want to switch, set

self.tableView.dataSource = self.dovizDataSource;

or

self.tableView.dataSource = self.altinDataSource; 

then

self.tableView reloadData;

See UITableView docs, UITableViewDataSource protocol reference.

Upvotes: 2

Crazyrems
Crazyrems

Reputation: 2591

First problem here:

self.viewController = [self.storyboard instanciateViewControllerWithIdentifier:@"TableView"];

You instantiate manually your view controller. The storyboard already manages this allocation for you, so after your viewDidLoad, you have your TableView displayed by the system with no reference to it, and you have a reference to another TableView not displayed.

Instead, remove the line I mentioned above and catch the view controller when it's instantiated by the system, in prepareForSegue

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    self.viewController = segue.destinationViewController;
}

You should name your segue by clicking on it and change the identifier in the attribute inspector, in case you have several segues in your viewController. You can get their name with segue.identifier and do actions depending on.

Second: I don't see any code that reloads your data

You may want to recall APIClient GetInformationFrom:self.URL… and in the completion block call [self.viewController.tableView reloadData] to make sure you have your new data before reloading.

Upvotes: 1

Related Questions