0

Below is the sequences how my app is loading data into my UITableView:

  1. UITableView loads from empty array.
  2. App fetches data asynchronously.
  3. After data is fetched and processed, app calls UITableView reloadData.

The problem I'm currently encountered is, I need to slightly slide the UITableView manually, in order to enable UITableView display the fetched data.

Currently I'm using iOS simulator to do the testing.

Is this considered normal? Is there anything I can do to display the data without a request from user to slide it?

-- EDIT --

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *tableCellIdentifier = @"TempTableViewCell";

    TempTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableCellIdentifier forIndexPath:indexPath];

    int row = [indexPath row];
    YoutubeVideo *currentVideo = [videoArray objectAtIndex:row];

    cell.TempLabel.text = currentVideo.VideoTitle;
    [cell.TempImage setImageWithURL:[NSURL URLWithString:currentVideo.VideoThumbnailURL] placeholderImage:[UIImage imageNamed:@"Icon-Small"]];
    cell.TempLabelDate.text = currentVideo.VideoDate;
    return cell;
}
WenHao
  • 1,183
  • 1
  • 15
  • 46
  • Is your sliding causes reuse of cells? In other words, are there any replaced cells while you're sliding? – EDUsta Aug 06 '14 at 16:20
  • I have added the code for the cellForRowAtIndexPath. – WenHao Aug 06 '14 at 16:31
  • Can you try setting images and texts in `tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath`? – EDUsta Aug 06 '14 at 16:34
  • Is still the same. Did you encounter something like this before? – WenHao Aug 06 '14 at 16:40
  • I think you can follow the solution give here http://stackoverflow.com/questions/2770158/how-to-scroll-to-the-bottom-of-a-uitableview-on-the-iphone-before-the-view-appea – Naga Mallesh Maddali Aug 06 '14 at 16:41

1 Answers1

2

If I understand your question properly, then...

I would guess that when you call [tableView reloadData], you are not on the main thread. Try this:

dispatch_async(dispatch_get_main_queue(), ^{
    [tableView reloadData];
});

Edit with clarification:

The reason for this is that your network request is probably occurring on a different thread, and, once you've finished and you reload the table, you are still on this background thread. When performing UI updates, you need to do it on the main thread, otherwise you get weird stuff happening , like you are seeing here.

The code above will perform the table update back on the main thread.

JoeFryer
  • 2,751
  • 1
  • 18
  • 23
  • Totally awesome .. it is working now.. is really different from android... looks like I will encounter all sort of weird stuff .. :P – WenHao Aug 06 '14 at 16:51