I have a standard UITableView with standard cells (meaning no modification). Each cell needs to have its text pull from a different web URL:
cell.textLabel.text = [self getTitleFromURL:myURL];
Of course, calling a URL on the main thread is not what you want to do. So how do I do this? I’ve tried something like (which I got from another StackOverflow post) this but it doesn’t work:
dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *title = [self getTitleFromURL:myURL];
dispatch_async( dispatch_get_main_queue(), ^{
cell.textLabel.text = title;
});
});
Ideas? I am probably missing something really simple here.
One thing you need to take care of with asynchronous data loading into tables – by the time the data arrives, the cell may have scrolled offscreen, and worse, may have been reused for a different entry in the data array.
So on your return to the main thread you need to check whether the information is still relevant, and make no assumptions as to which cell to post it to…
You also may need to tell the cell to update itself with setNeedsLayout.