【问题标题】:Help With NSNotifcation and Asynchronous Downloading帮助 NSNotifcation 和异步下载
【发布时间】:2011-05-31 05:39:21
【问题描述】:

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

谢谢

MKDev

【问题讨论】:

  • xcode只是一个IDE,和这个问题无关

标签: iphone objective-c cocoa-touch nsxmlparser nsnotificationcenter


【解决方案1】:

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

因此,您需要在第 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]];
   }
   ...
}

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

【讨论】:

  • 我的问题是图片下载后 tableView 没有重新加载。 tableView 只有在滚动时才会重新加载。你知道如何解决这个问题吗?
  • 但您确定在两者之间调用了“reloadTableView”?我再次修改了我的代码 - 替换了 reloadData。我在代码中使用这种转换来切换到不同的单元格样式进行编辑。
  • reloadTableView 仅在 tableview 滚动时被调用。
  • 这听起来很奇怪。来电者是谁?从您的代码中,它应该只在发送通知时调用。也许你应该尝试重命名它,以防它与 UITableView 方法发生冲突?
  • 调用者在cellForRowAtIndexPath方法中:[asyncImage loadImageFromURL:[NSURL URLWithString:pathImage]];
【解决方案2】:

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

【讨论】:

  • 啊。基本上 tableView 需要在 connectDidFinishLoading 时重新加载,所以我试图从 asyncView 向 rootviewcontroller 发送通知以告诉 tableView 重新加载。请你告诉我如何在不等到表格视图滚动的情况下做到这一点?
猜你喜欢
  • 2011-03-14
  • 1970-01-01
  • 2011-02-08
  • 1970-01-01
  • 2011-11-26
  • 2013-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多