mercredi 24 juin 2015

Why does my UITableView content 'jump up' when trying to scroll up the table?

I have a really weird UITableView issue. Upon initial load, it'll fetch say 10 rows. Everything is fine. I can scroll down and up.

However, if I call an additional data fetch to be added to the current data, I get an issue when trying to scroll the tableview back up.

Here's what I do

  • Fetch 10 rows on viewDidLoad. Scroll to bottom, and wait...
  • After 5 seconds, 10 more rows will be added via -performSelector: withObject: afterDelay:
  • Attempt to scroll back up (finger moving down, showing above content, rows 9, 8, 7, ... 2, 1).
  • However, after this second fetch, the UITableView no longer scrolls properly. It jumps like crazy. For example, say the tableView is 500 height. And the top of the 9th row is at Y=50. I draw my finger down 10px, and the top of the 9th row is Y=60, and the bottom of the 8th is starting to reveal itself. I continue dragging my finger in one slow and continuos pace. 20px, 30px, 40px, etc. All of a sudden, at some random point, say 100px (Top of 9th row is at Y=150), BOOM, the content jumps. The top of the 9th row is no longer at Y=150, but at perhaps Y=70. This happens over and over. Continue to drag your finger down, the content will come with for a bit, and then seemingly at random, it jumps back up.

Have you ever seen this?

A couple of things

1) I can rule out it being the # of cells in the table. If I decide to fetch 25 rows on first fetch it'll be fine. It's not until I do that second fetch that this becomes an issue. (Making think it can't likely be anything in the uitableview delegate/datasource methods). If I fetch 5 rows on first fetch, it'll be fine. Fetch 5 more 5 seconds later, and the issue begins.

Other relevant information could be that I'm using self sizing cells.

Here's the relevant code I would think. Any ideas?

- (void)viewDidLoad
{
    [super viewDidLoad];
    ...
    [self fetchQuestionsAndClearExistingOnSuccess:NO];
    [self performSelector:@selector(loadMore) withObject:self afterDelay:7.0];
}

- (void)fetchQuestionsAndClearExistingOnSuccess:(BOOL)clear withCompletion:(void (^)(void))completion 
{
    NSString *userIdParam = [NSString stringWithFormat:@"user_id=%@&page=%d", (User *)[[UserManager sharedManager] getUser].userId, self.nextPageToRequest];

    [[ServerAccessAndCoreDataLayer sharedCDL] getQuestionsWithParamString:userIdParam andCompletion:^(BOOL success, NSArray *resultArray, NSInteger statusCode, NSArray *messages, NSArray *errors) {
        if (completion) {
            completion();
        }

        if (success) {
            if (clear) {
                [self.questions removeAllObjects];
            }
            self.nextPageToRequest++;
            [self loadQuestions:resultArray];
        }
        else {
            [self.tableView reloadData];
        }
    }];
}

- (void)loadQuestions:(NSArray *)resultArray 
{
    if (resultArray.count > 0) {
        for (Question *question in resultArray) {
            if (![self.questions containsObject:question]) {
                [self.questions addObject:question];
            }
        }

        [self.tableView reloadData];
    }
    else {
        self.endOfQuestionsReached = YES;
    }
}

See more ..

Aucun commentaire:

Enregistrer un commentaire