views:

537

answers:

3

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

A: 

Wow, that's freaky.

The last time I did something like this, I simply used multiple views, hiding one and showing another when the segmented control was tapped. There are other possible solutions, but this is probably easiest and perhaps most efficient.

Nimrod
Yeah, I was doing this, but then you cannot scroll up via the statusBar and I was having problems pushing views.
Canada Dev
+1  A: 

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.

Corey Floyd
A: 

The table view is caching the cells internally it uses for displaying your data. So if you change the data source you should also check that your is - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method is updating all the cells to the correct new values.

From your description it sounds like it is using the cached UITableViewCell instances and is not updating it to the correct new data in all cases. Perhaps code like this:

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.frame = CGRectZero;
        cell.textLabel.font = //Set font;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.text = @"My Text for this cell"; // <==== Not good! Move it out of this if block
    }
    // Set cell text here
}

The simplest solution I found for this sort of problem is to just make the String you use for the cell creation (CellIdentifier) depending of the data Source. In this case you don't mix the cell of the different content types (and this helps you also if the cells need to have a different look depending on the mode).

Chris