开发者

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 = profileT开发者_如何学GoableView;
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


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;


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.


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


I had this problem, turned out it was because my CellIdentifiers were the same...

static NSString *CellIdentifier = @"Cell";

Change one of them and the cells layout properly

static NSString *CellIdentifier2 = @"Cell2";


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.


I have the same issue and what you have to do is use a different cell identifier within - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath.

if (self.segmentedControl.selectedSegmentIndex == 0) {
    self.cellIdentifier = @"segmentOne";
} else {
    caseAtIndexPath = [self.awaitingReviewCaseList caseAtIndex:indexPath.row];
    self.cellIdentifier = @"segmentTwo";
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier forIndexPath:indexPath];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜