UITableView中的单元格imageView不会出现,直到选定

  1. 这是一个问题的video:http: //youtu.be/Jid0PO2HgcU

  2. 尝试从资源库设置UITableView中的[cell imageView]图像时出现问题。

图像被加载,但它不会出现在单元格中,直到我触摸(select)该单元格。 这是我的代码,设置单元格的imageView:

//Start loading the image using the image path NSString *path = [occasion imagePath]; if (path != nil && [path hasPrefix:@"ass"]) { NSLog(@"photo loaded from assets library"); //testing ALAssestLibrary ALAssetsLibrary* assetsLibrary = [[ALAssetsLibrary alloc] init]; ALAssetsLibraryAssetForURLResultBlock resultsBlock = ^(ALAsset *asset) { ALAssetRepresentation *representation = [asset defaultRepresentation]; CGImageRef imageRef = [representation fullResolutionImage]; UIImage *image = [[UIImage alloc] initWithCGImage:imageRef]; [[cell imageView] setImage:[image imageByScalingAndCroppingForSize:CGSizeMake(36.0, 42.0)]]; [image release]; }; ALAssetsLibraryAccessFailureBlock failureBlock = ^(NSError *error){ NSLog(@"FAILED! due to error in domain %@ with error code %d", error.domain, error.code); // This sample will abort since a shipping product MUST do something besides logging a // message. A real app needs to inform the user appropriately. abort(); }; //Convert path to an NSUrl NSURL *url = [NSURL URLWithString:path]; // Get the asset for the asset URL to create a screen image. [assetsLibrary assetForURL:url resultBlock:resultsBlock failureBlock:failureBlock]; // Release the assets library now that we are done with it. [assetsLibrary release]; } 

以上代码是以下代码的一部分:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

有任何想法吗?

我想在这种情况下,你使用的是iOS定义的UITableViewCell(而不是自定义的),这是你从“initWithStyle:reuseIdentifier:”获得的单元格之一。 现在,这些单元格内部构build的方式是,如果不立即分配一些内容,则相应的子视图将不会添加到单元格的contentView层次结构中。 这种行为是典型的复杂的自定义视图,其中最后的单元格布局只有当其所有子视图的内容完全指定时才能确定。 (例如,您有两个标签,假设标签A和标签B,标签A是多行,并且您希望在标签-A的正下方添加标签-B,则只有标签为A,才能正确放置标签B.并计算其高度)。 所以我想内置的iOS表格单元格是以相同的方式完成的,子视图层次结构是在“layoutSubviews”阶段构build的。

在你的情况下,你创build单元格,但推迟图像提供的时间。 但在这一点上,layoutSubviews已经被调用,没有任何图像,相应的imageView甚至没有被分配和添加在contentView层次结构中!

所以,据我所说,解决scheme只是为您的资产立即分配一个“占位符”(占位符可以是一个透明的图像当然),这将保证你的内部UIImageView创build。 出于上面解释的相同原因,小心图像大小,应该接近预期的图像大小。 一旦你的完成块被称为图像视图应该已经在那里,图像将出现。

当你点击单元格图片时,原因是这个操作再次调用layoutSubviews。 在这种情况下,可能会重新执行整个代码,并且在设置内部“image”属性之前,您已经调用了“setImage:”,contentView层次结构将与新图像一起重build。

你的问题的另一个可能的解决scheme当然是使用“自定义”UITableViewCell(例如从一个笔尖加载)。 在这种情况下,你所有的子视图都会被加载,你只需要使用他的标签访问UIImageView(阅读苹果文档,了解更多关于这个有用的技巧来从Nib文件创build表视图单元格)。

你需要设置图像后的布局单元格

就像

[cell setNeedsLayout];

我挠了一下脑袋,终于弄明白了。

我的错误是,我设置图像在cell.imageView当我应该设置我的实际出口cell.eventImageView 。 这是在UITableViewCell提供的通用imageview搞乱。 希望它有助于某人。

这是我的方式我如何解决这些问题,我知道不是最好的,但那工作:)

  UIImage *thumbnailImage = ... dispatch_async(dispatch_get_main_queue(), ^{ [cell.imageView setImage:thumbnailImage]; UITableViewCellSelectionStyle selectionStyle = cell.selectionStyle; cell.selectionStyle = UITableViewCellSelectionStyleNone; [cell setSelected:YES]; [cell setSelected:NO]; cell.selectionStyle = selectionStyle; }); 

在我的情况下,我通过Storyboard设置了一个原型单元格,但意外地使用了dequeueCellWithIdentifier:forIndexPath方法而不是dequeueCellWithIdentifier:

第一种方法只是通过编程的UITableView registerClass:forReuseIdentifier:registerNib:forReuseIdentifier方法将单元格挪用到tableview中registerNib:forReuseIdentifier使用。

我有一个类似的问题。 我已经注册了默认的UITableViewCell类而不是我的自定义类。

我有这个:

 tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: visibilityCellIdentifier) 

但我应该有这样的:

 tableView.registerClass(VisibilityCell.self, forCellReuseIdentifier: visibilityCellIdentifier) 

当我通过debugging器的时候,单元格的创build看起来都在工作,但是直到我select了每一行之前,什么也没有显示出来。

有时它会发生,当你使用自定义的UITableViewCell与标签,图像等,但也是你设置单元格的默认UILabel的地方。 例如

 customCell.textLabel.text = @"some text" 

为了解决这个问题,避免使用默认标签,而是在自定义单元格中添加自己的UILabel。

我使用此代码时遇到同样的问题:

 cell = [tableView dequeueReusableCellWithIdentifier:kCellSection1 forIndexPath:indexPath]; if (indexPath.row == 0) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellSection1]; cell.textLabel.text = @"Vote Up for me if it help for you!"; } 

但我通过replace来修复它:

 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellSection1]; 

至:

 cell = [cell initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellSection1];