Ios: reloading UITableViewData not working until scroll

I noticed a few questions like this in SO, but they all seem to end up with the person calling

 [self.tableView reloadData]

Out of the main stream.

My problem is that I call this in the main UI thread, and I still don't load new lines until I like to use the screen or scroll a bit. Here's what I do: when I enter my ViewController, the table is displayed fully and correctly. After clicking the button on the view, I call my external db and load some new rows that need to be added to the table. After adding them to my data source, I call reloadData, and all new lines are empty. A couple of rows that still fit on the screen that were already part of the table are still displayed, but nothing new. When I touch the screen or scroll a bit, all new cells appear.

Here is my code, starting from the end of my call to the server:

     - (void)connectionDidFinishLoading:(NSURLConnection *)connection {

if(connection == self.searchConnection) {

 ..... some code ......

    if(successful) {
        // Adding new data to the datasource
        [self.locations removeObjectAtIndex:[[NSNumber numberWithInt:self.locations.count - 1] integerValue]];
            [self.cellTypeDictionary removeAllObjects];
            for(int i = 0; i < [jsonLocations count]; ++i) {
                NSDictionary *locationDictionary = jsonLocations[i];
                BTRLocation *location = [BTRLocation parseLocationJSONObject:locationDictionary];
                [self.locations addObject:location];
            }


        if(self.currentNextPageToken != nil && ![self.currentNextPageToken isEqual:[NSNull null]] && self.currentNextPageToken.length > 0) {
            BTRLocation *fakeLocation = [[BTRLocation alloc] init];
            [self.locations addObject:fakeLocation];
        }

        [self determineCellTypes];
        if([self.locations count] < 1) {
            self.emptyView.hidden = NO;
        } else {
            ... some code .....

            if(self.pendingSearchIsNextPageToken) {
                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[[NSNumber numberWithInt:countBefore] integerValue]

                                                            inSection:[[NSNumber numberWithInt:0] integerValue]];

                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.tableView reloadData];
                    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
                });
            } else {
                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[[NSNumber numberWithInt:0] integerValue]
                                                            inSection:[[NSNumber numberWithInt:0] integerValue]];

                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.tableView reloadData];
                    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
                });
            }
        }
    } else {
        if(invalidAccessToken) {
            // TODO: invalidAccessToken need to log out
        }
    }

You can see that I even added

  dispatch_async(dispatch_get_main_queue(), ^{
                    [self.tableView reloadData];
                    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
                });

, connectionDidFinishLoading . , .

, .

+4
1

:

[self.tableView reloadData];
__weak MyViewController* weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
                 __strong MyViewController *strongSelf = weakSelf;
                [strongSelf.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
            });

, reloadData:

[self.tableView layoutIfNeeded];
0

All Articles