Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView doesn't reload properly on iOS 7.1

I have a UIViewController which has a UITableView and a UISegmentedControl with 4 segments in it. Every time the user taps on a different segment, the type of cells shown in the table view should change. To do this, I have the following code:

- (IBAction)segmentedControlChanged:(UISegmentedControl *)sender {
    [self.tableView reloadData];
}

Usually, the third segment populates the table view with the greatest number of cells. So, while on the third segment, when I scroll to the bottom of the table and then switch to another segment, I expect the table view to just reload its cells and stay at the same scroll position. However, what happens is that the header view seems to be positioned at the bottom of the screen as if the table view was scrolled to the top to its limits. Then, when I touch and start scrolling again, things go back to normal as if the table was just scrolled downwards and the cells appear.

I've been trying to solve this problem for a while now and the posts I've seen so far all suggested the same thing. They said that if the animation is handled in a thread other the main thread, this type of problems were inevitable. So I tried modifying the code like below:

- (IBAction)segmentedControlChanged:(UISegmentedControl *)sender {
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
    });
}

and:

- (IBAction)segmentedControlChanged:(UISegmentedControl *)sender {
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        [self.tableView reloadData];
    }];
}

Neither of them worked. I also tried scrolling the table view programmatically after reload bu that didn't seem to work either.

Anyone have any other ideas why this might occur? I should note that this problem only occurs on iOS 7.1.

like image 452
halileohalilei Avatar asked Nov 10 '22 22:11

halileohalilei


1 Answers

As I understand it, you are only getting the issue when transitioning away from segmented control index filter with the largest number of results.

When you tried scrolling the table view programmatically after reload, did you calculate what the index path would be for the target result set for segmented control 1 or 2?

Example:

If we are moving from segmentedControlIndex 3 to 1 where 3 has 30 records and 1 has 10 records:

[self.tableView reloadData]
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:10 inSection:0];
[self.tableView scrollToRowAtIndexPath:indexPath
                atScrollPosition: UITableViewScrollPositionBottom
                        animated:YES];

The above assuming you have 1 section.

like image 96
applejack42 Avatar answered Jan 04 '23 01:01

applejack42