0

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

谢谢

MKDev

4

2 回答 2

0

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

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

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

于 2010-12-27T18:51:17.847 回答
0

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

于 2010-12-27T17:28:03.487 回答