我有一个自定义的UITableViewCell类,其模型对象执行要在单元格中显示的图像的异步下载。我知道我已经在IB中为WidgetTVC正确连接了插座,我知道图像数据正在从我的服务器正确返回,我也分配/初始化了widget.logo UIImage。为什么在我的tableViewCell中图像总是空白?提前谢谢。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
Widget *theWidget = [widgetArray objectAtIndex:indexPath.row];
static NSString *CellIdentifier = @"WidgetCell";
WidgetTVC *cell = (WidgetTVC*)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
[[NSBundle mainBundle] loadNibNamed:@"WidgetTVC" owner:self options:nil];
cell = self.widgetTVC;
self.widgetTVC = nil;
}
[cell configureWithWidget:theWidget];
return cell;
}
在我的WidgetTVC课程中,我有以下方法:
- (void)configureWithWidget:(Widget*)aWidget {
self.widget = aWidget;
self.nameLbl.text = aWidget.name;
[self.logoIvw setImage:aWidget.logo]; // logoIvw is the image view for the logo
}
最后 - 我有一个回调方法,它以异步方式(简化)在Widget模型对象上设置徽标UIImage属性:
(void)didReceiveImage:(ASIHTTPRequest *)request {
// I pass a ref to the Widget's logo UIImage in the userInfo dict
UIImage *anImage = (UIImage*)[request.userInfo objectForKey:@"image"];
UIImage *newImage = [UIImage imageWithData:[request responseData]];
anImage = newImage;
}
答案 0 :(得分:0)
在didReceiveImage:
中,您只修改本地指针anImage
。您需要设置image
的{{1}}属性,以便更新显示的内容。不是存储对小工具UIImageView
的引用,而是传递对UIImage
的引用,而在UIImageView
中执行类似
didReceiveImage:
答案 1 :(得分:0)
也许最好的解决方案是让你的模型对象将图像作为属性,显示对象通过KVO订阅图像属性的更改,并在图像属性发生变化时自行更新。
答案 2 :(得分:0)
首先,我向Widget对象添加了一个方法,以异步方式获取徽标:
- (void)asyncImageLoad {
...
// logo is a UIImage
[AsyncImageFetch fetchImage:&logo fromURL:url];
}
我自己的AsyncImageFetch类看起来像这样:
+ (void)fetchImage:(UIImage**)anImagePtr fromURL:(NSURL*)aUrl {
ASIHTTPRequest *imageRequest = [ASIHTTPRequest requestWithURL:aUrl];
imageRequest.userInfo = [NSDictionary dictionaryWithObject:[NSValue valueWithPointer:anImagePtr] forKey:@"imagePtr"];
imageRequest.delegate = self;
[imageRequest setDidFinishSelector:@selector(didReceiveImage:)];
[imageRequest setDidFailSelector:@selector(didNotReceiveImage:)];
[imageRequest startAsynchronous];
}
+ (void)didReceiveImage:(ASIHTTPRequest *)request {
UIImage **anImagePtr = [(NSValue*)[request.userInfo objectForKey:@"imagePtr"] pointerValue];
UIImage *newImage = [[UIImage imageWithData:[request responseData]] retain];
*anImagePtr = newImage;
}
最后,根据Ed,我将其添加到configureWithWidget方法中,该方法有助于设置我的WidgetTVC:
[aCoupon addObserver:self forKeyPath:@"logo" options:0 context:nil];
当观察到变化时,我更新了imageView并调用[self setNeedsDisplay]。奇迹般有效。我能以任何方式给你这两点吗?