Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView isn't refreshing properly after changing dataSource

I have a UITableView with several datasources. This is because, I need to switch the data with a UISegmentedControl, and if I add them as subviews, I cannot use the statusBar to scroll up, etc.

At first, I show a login screen:

self.tableView.dataSource = loginTableView;
self.tableView.delegate = loginTableView;
[self.tableView reloadData];

Then, once the user has logged in, I do the following to change to index:1 of the segmentedControler, which is their profile:

self.tableView.dataSource = profileTableView;
self.tableView.delegate = profileTableView;
[self.tableView reloadData];

However, the table view updates, but is a bit of a mix between the two dataSources. Some of the text has changed, some is overlapping, while some of it is completely missing.

Is there another way I should be changing the dataSource for a UITableView?

Thx

like image 448
runmad Avatar asked Jan 14 '10 18:01

runmad


2 Answers

I had the exact same problem. My solution was to make the tableView hidden, change it's source, reload the data and make the tableView visible again.

Example in C# (MonoTouch):

tableView.Hidden = true;
tableView.Source = newTableViewSource;
tableView.ReloadData();
tableView.Hidden = false;
like image 164
Robert Ros Avatar answered Oct 19 '22 17:10

Robert Ros


Not sure why this is happening from the code you have posted.

Instead of changing the delegate and datasource, swap out whatever ivar represents the data being displayed:

- (NSArray*)tableData{

    if(showingLogin)
        return self.loginData;

    return self.profileData;
}

Now you only have 1 UITableViewController instance, but a BOOL to tell you which datasource to use.

like image 40
Corey Floyd Avatar answered Oct 19 '22 19:10

Corey Floyd