开发者

reloadRowsAtIndexPaths stops loading UITableViewCells?

开发者 https://www.devze.com 2023-03-09 08:32 出处:网络
I\'m trying to incorporate favicons into a UITableView.The table basically fetches websites, and I want to display the favicon onto the right.I put a placeholder icon at the right initially and let a

I'm trying to incorporate favicons into a UITableView. The table basically fetches websites, and I want to display the favicon onto the right. I put a placeholder icon at the right initially and let a function in the background run. This function takes the URL of the website, parses it and attempts to find the favicon. If it can't find it, it keeps the same placeholder image; otherwise, it replaces it with the site's favicon. I initially tried using [tableView reloadData] which worked well in the simulator, but it did really odd and unreliable things (like for instance, it would create some cells, but then leave a giant, blank cell). Anyway, I stumbled upon reloadRowsAtIndexPaths, and it seems like the function I need to use. However, the results are still pretty unreliable. I have my fetching function running in the background as such:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...//other code here for showing website labels

    //obtain the favicon.ico
if(!this_article.iconLoaded){
    this_article.iconLoaded = TRUE;
    NSDictionary *args = [NSDictionary dictionaryWithObjectsAndKeys:this_article, @"article", indexPath, @"indexPath", nil];
    [self performSelectorInBackground:@selector(fetchFav开发者_如何学GoiconWrapper:) withObject:args];
}

cell.favicon.image = this_article.icon;

return cell;
}

in FetchFaviconWrapper:

- (void)fetchFaviconWrapper:(NSDictionary *)args {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[self fetchFavicon:[args objectForKey:@"article"]];
NSArray *paths = [NSArray arrayWithObject:[args objectForKey:@"indexPath"]];
[articleTable beginUpdates];
[articleTable reloadRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationFade];
[articleTable endUpdates];
[pool release];
}

Basically, fetchFavicon takes a website, takes the host URL, appends "/favicon.ico", constructs it into an NSData object, and finds the image (if it exists). However, this has also been pretty unreliable. What would be the best way to replace the placeholder image while running a thread in the background? I could do everything on the main thread, but that just makes the table load slowly. There seems to be something that I'm overlooking, or something that I just forgot to add...just can't figure it out.


It is not entirely clear if you are accessing and modifying your UI from the separate thread.

If so, this is the cause of your unreliability. UIKit can be accessed only from the main thread. If you are interested you will find many questions on S.O. and many discussions on the web.

There is a workaround, if you want to keep your second thread. Indeed, you can send messages to your UI object using:

-performSelectorOnMainThread:withObject:waitUntilDone:

instead of sending them directly from the secondary thread.

If this workaround does not solve the issue for you, then I would suggest redesigning your app so that the secondary thread only access your model, without accessing the UI. All the operations that modify the UI should be executed on the main thread. If you need to call reloadData on your table when the model is ready, you can do it using performSelectorOnMainThread:withObject:waitUntilDone.


I'd suggest a few things.

  1. Appending ./favicon.ico isn't always accurate. Look into various methods of adding favicons to sites to support them.

  2. As far as replacing the default placeholder, I suggest using the NSNotificationCenter to inform the main thread when to make the changes.


I definitely reiterate the no UI from background thread.

change your initial code:

- (void)fetchFaviconWrapper:(NSDictionary *)args {
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  [self fetchFavicon:[args objectForKey:@"article"]];
  NSArray *paths = [NSArray arrayWithObject:[args objectForKey:@"indexPath"]];

  [articleTable beginUpdates];
  [articleTable reloadRowsAtIndexPaths:paths 
                      withRowAnimation:UITableViewRowAnimationFade];
  [articleTable endUpdates];

  [pool release];
}

to this, and see if you're still having the problem

- (void)fetchFaviconWrapper:(NSDictionary *)args {
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  [self fetchFavicon:[args objectForKey:@"article"]];
  NSArray *paths = [NSArray arrayWithObject:[args objectForKey:@"indexPath"]];

  [self performSelectorOnMainThread:@selector(reloadData) 
                         withObject:nil 
                      waitUntilDone:YES];

  [pool release];
}

The waitUntilDone is irrelevant in this case, and it's preferred form to say YES.

Another problem to suspect when you have funky cell behavior is if you are correctly handling cell reuse. You didn't show us that code, so we can't tell. If you are keeping a reference to a cell somewhere and setting the image data into that - you'll hose yourself when the table reuses the cell out from under you. (I don't think that's the problem in this case.)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号