0

底部更新

为此,我从 MKNetworkKit Flickr 演示中分离出来。我想在表格中显示的网络服务器上有几张图像。我有一个 UITableViewCell 子类 ImageCell。

这是检索远程图像的自定义方法:

-(void)setImageForCell:(NSString *)remoteFileName {

    self.loadingImageURLString =
    [NSString stringWithFormat:@"http://myserver.com/%@.png",remoteFileName];
    self.imageLoadingOperation = [ApplicationDelegate.imageDownloader imageAtURL:[NSURL URLWithString:self.loadingImageURLString]
                            onCompletion:^(UIImage *fetchedImage, NSURL *url, BOOL isInCache) {

                                if([self.loadingImageURLString isEqualToString:[url absoluteString]]) {

                                    if (isInCache) {
                                        self.imageView.image = fetchedImage;
                                        [self.contentView drawRect:self.contentView.frame];
                                        NSLog(@"Image is in cache");
                                    } else {
                                        self.imageView.image = fetchedImage;
                                        [self.contentView drawRect:self.contentView.frame];
                                        NSLog(@"Image is not in cache");
                                    }
                                }
                            }];   
}
    //TableView.h
    //it is called like this
   //in cellForRowAtIndexPath...
   ImageCell *cell = (ImageCell *)[tableView dequeue...etc];
 MyObject *obj = [dataSource objectAtIndex:indexPath.row];
 cell.textLabel.text = obj.name;
 [cell setImageForCell:obj.name];
 return cell;

我检查了我的默认缓存目录的内容,现在里面有项目。现在不断滚动表格会打印“图像在缓存中”。问题是,单元格的 imageView 永远不会更新。Mugunth 有一个类方法 automaticallyNotifiesObserversForKey: 但我从来没有看到它在任何地方实现。我猜还有另一个步骤是告诉 tableView 实例用新内容更新该行。

感谢您的输入。

更新

我通过使用带有 Cell 和 UIImageView IBOutlet 的自定义 Interface Builder xib 文件来实现这一点。不知道为什么它不能与 self.imageView.image 一起使用,并且有兴趣确切地找出原因。我仍然认为这是一个悬而未决的问题,因为我只想使用标准的 UITableViewCell 类。

4

2 回答 2

3

我调用prepareForReuse。您是否将 self.imageView.image 设置为零?如果是,请不要这样做,因为它看起来从未重新初始化。

于 2012-11-09T02:06:46.570 回答
1

你为什么从你的代码中调用“drawRect”。那是亵渎!检查您的 imageView 并检查 IB 连接是否良好。

于 2012-08-13T02:37:15.500 回答