NSNotifcation 和异步下载帮助

发布于 2024-10-09 23:24:17 字数 228 浏览 3 评论 0原文

我正在从一个视图向另一个视图发送通知。我的问题是,我在 cellForRowAtIndexPath 方法中调用的视图中的通知仅在表视图滚动时才会发送。我怎样才能阻止这个并让它在图像下载后发送通知?这是我的代码: https://gist.github.com/756302

谢谢

MKDev

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

Thanks

MKDev

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迟到的我 2024-10-16 23:24:17

据我了解您的代码,该消息将触发整个表的重新加载。这应该会导致单元格的刷新。

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

我想到检查这一点的第一件事是在 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]];
   }
   ...
}

注意,重新加载表可能还有其他原因 - 视图可能已消失或卸载,而您可能不会希望多次触发异步加载。

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.

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.

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];
}

and then to add in

- (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.

败给现实 2024-10-16 23:24:17

您的代码应该可以正常工作,当 connectionDidFinishLoading 时,您调用 NSNotificationCenter 发送通知,cellForRowAtIndexPath 中没有 post 方法

Your code should work right, when the connectionDidFinishLoading, you call the NSNotificationCenter to send the notification, there is no post method in cellForRowAtIndexPath

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文