自定义 UITableViewCell imageView 在 setImage 后未更新
我有一个自定义 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;
}
I've got a custom UITableViewCell class whose model object performs an asynchronous download of an image which is to be displayed in the cell. I know I've got the outlets connected properly in IB for WidgetTVC, I know that image data is being properly returned from my server, and I've alloc/init'd the widget.logo UIImage too. Why is the image always blank then in my tableViewCell? Thanks in advance.
- (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;
}
In my WidgetTVC class, I have the following method:
- (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
}
Finally- I've got the callback method that sets the logo UIImage property on the Widget model object asynchronously (simplified):
(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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在
didReceiveImage:
中,您仅修改本地指针anImage
。您需要设置UIImageView
的image
属性才能更新显示的内容。不要隐藏对小部件的UIImage
的引用,而是传递对UIImageView
的引用,并在didReceiveImage:
中执行类似的操作In
didReceiveImage:
, you're only modifying the local pointeranImage
. You need to set theimage
property of yourUIImageView
in order to update what gets displayed. Instead of stashing a reference to your widget'sUIImage
, pass a reference to theUIImageView
, and indidReceiveImage:
do something like也许最好的解决方案是让模型对象将图像作为属性,并且显示对象通过 KVO 订阅图像属性的更改,并在图像属性发生更改时更新自身。
Perhaps the best solution would be to have your model object have the image as a property and the display object subscribe to the image property's changes through KVO and update itself whenever the image property changes.
好吧-这里有一些问题。感谢 bosmacs 和 Ed Marty,你们的评论都被用来找到解决方案。
首先,我向 Widget 对象添加了一个方法来异步获取徽标:
我自己的 AsyncImageFetch 类如下所示:
最后,根据 Ed,我将其添加到 configureWithWidget 方法中,以帮助设置我的 WidgetTVC:
当观察到更改时,我更新 imageView 并调用 [self setNeedsDisplay]。就像魅力一样。有什么办法可以给你两点吗?
OK- there were a couple things wrong here. Thanks to bosmacs and Ed Marty as both of your comments were used to get to the solution.
First, I added a method to the Widget object to get the logo asynchronously:
And my own AsyncImageFetch class looks like this:
Finally, per Ed, I added this to the configureWithWidget method that helps set up my WidgetTVC:
And when a change is observed, I update the imageView and call [self setNeedsDisplay]. Works like a charm. Any way I can give you both points?