帮助NSNotifcation和异步下载 [英] Help With NSNotifcation and Asynchronous Downloading

查看:60
本文介绍了帮助NSNotifcation和异步下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从一个视图向另一个视图发送通知。我的问题是,仅在tableview滚动时,才会在cellForRowAtIndexPath方法中调用的视图中的通知被发送。图像下载后,如何停止此操作并使其发送通知?这是我的代码: https://gist.github.com/756302

I am sending a notification from one view to another view. My problem is that the notification in the view that I am calling in my cellForRowAtIndexPath method is only getting sent when the tableview is scrolling. How can I stop this and make it send the notification once the images have downloaded? Here is my code: https://gist.github.com/756302

谢谢

MKDev

推荐答案

据我了解您的代码,该消息将触发整个表的重新加载。那应该导致细胞的刷新。

as far as I understand your code, the message will trigger the reload of the whole table. That should lead to a refresh of the cells.

因此,您需要在第76行中检查是否正在绘制单元格,因为完成消息触发了重新加载(并且图像现已准备就绪)显示),或者如果您需要启动图像的异步下载。

Thus, you'll need to check in line 76, if the cell is being drawn because a reload was triggered from the finish-message (and the image is now ready to display) or if you need to start the asynchronous download of the image.

我想检查的第一件事是在reloadTableView中设置一个属性:

The first thing which comes into my mind to check this is to set a property in reloadTableView:

- (void)reloadTableView
{
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"aaa"];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"name" object:nil];
    NSLog(@"removeobserver");
    loadImageFinished = YES;
    // if your table has several sections you'll need to adopt the section number 
    NSIndexSet *indices = [[NSIndexSet alloc] initWithIndex:0];
    [self.tableView reloadSections:indices withRowAnimation:UITableViewRowAnimationFade];
    [indices release];
}

然后添加

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   ...
   if (loadImageFinished) {
      ... 
   } else {
      [asyncImage loadImageFromURL:[NSURL URLWithString:pathImage]];
   }
   ...
}

请注意是重新加载表的其他原因-视图可能已经消失或卸载,并且您可能不希望多次触发异步加载。

Note that there could be other reasons why the table is being reloaded - the view could have been disappeared or unloaded and you might not wish to trigger your asynnchronous loading several times.

这篇关于帮助NSNotifcation和异步下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆