我有一个 UITableView,每个单元格中包含一些 View ,其中一个 View 是 UIImageView。 在 cellForRow 方法中,我使用图像加载 UIImageView,但有时,UIImageView 仅显示黑屏。
我认为这可能有两个原因 - UIImage 损坏或内存问题。
UIImage 没有损坏(始终是同一个 UIImage),我什至检查了当我放置手动损坏的图像时的行为 -> 它变成白色!不是黑...
我检查是否有一些内存警告,ViewDidUnload 启动 -> 什么都没有。即使在模拟器中模拟了内存警告,也没有什么,与此无关。
有人知道为什么会发生这种情况吗?
谢谢!
这是我的 cellForRow 方法。 基本上,它是顶部有 UIWebView 和 UIImageView 的单元格列表,当表格滚动时,我隐藏 webview 并显示 imageview,反之亦然。也许这与我的问题有关,但我对此表示怀疑,因为图像是相同的,并且 uiimagaview 没有发布。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ImMailMessage *message = [_messages objectAtIndex:indexPath.row];
ImFullMessageTableViewCell *cell = (ImFullMessageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"ImFullMessageTableViewCell"];
if (cell == nil)
{
cell = [[[NSBundle mainBundle] loadNibNamed:@"ImFullMessageTableViewCell" owner:self options:nil] objectAtIndex:0];
cell.delegate = self;
//add gestures
UIPinchGestureRecognizer *pinchGesture = [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(cellPinched:)] autorelease];
[cell addGestureRecognizer:pinchGesture];
pinchGesture.delegate = self;
UIPanGestureRecognizer *panGesture = [[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(cellPanned:)] autorelease];
panGesture.minimumNumberOfTouches = 2;
[cell addGestureRecognizer:panGesture];
panGesture.delegate = self;
}
cell.threadState = _state;
cell.mailMessage = message;
UIImage *image = [self imageForMailMessage:message];
if (!image)
{
// if no image, put the webview and reload it.
cell.webView.mailMessage = message;
cell.webViewImageView.hidden = YES;
cell.webViewImageView.image = nil;
[cell.webView reloadMailMessage];
}
else // image exists!
{
cell.webViewImageView.image = image;
// replace the web view with the recived image.
[cell replaceWebViewWithImageView];
}
return cell;
}
最佳答案
因此,在“cellForRow”中,您首先尝试回收旧单元格,然后如果没有可用的单元格,则创建一个新单元格。之后您就可以以某种方式访问 UIImageView (或者您正在使用单元格提供的那个?)
我要做的第一件事是在 cellForRow 中甚至不设置图像 - 找到 UIImageView,然后记录它或断言它在那里,然后将背景颜色设置为绿色”:
imageView.backgroundColor = [UIColor greenColor];
如果您在图像所在的位置看到一堆绿色矩形,那么您可以继续下一步(删除绿色背景后!),并设置图像 - 但每次设置图像时记录,即在设置之前记录图像,这样您就可以 100% 确定您确实拥有有效的图像。
如果您到目前为止,您已获得绿色,正在记录图像,但它仍然无法正常工作,请添加评论,我会回来查看。
编辑:所以继续不写图像 - 你现在应该有一个 imageView 只是绘制它的背景。
添加委托(delegate)方法:“- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath "
在那里放置一个日志,并为每个单元格记录其索引、imageView、imageView.backgroundColor 和 imageView.image(应该为零!)。我怀疑你会发现背景色不是绿色,或者有一些图像
关于iphone - UIImageView 不一致地变黑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11923160/