UITableView 中的单元格 imageView 直到被选中才会出现 [英] cell imageView in UITableView doesn't appear until selected

查看:26
本文介绍了UITableView 中的单元格 imageView 直到被选中才会出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 这里是问题的视频:http://youtu.be/Jid0PO2HgcU

我在尝试从资源库的 UITableView 中为 [cell imageView] 设置图像时遇到问题.

I have a problem when trying to set the image for the [cell imageView] in a UITableView from the asset library.

图像已加载,但在我触摸(选择)该单元格之前它不会出现在单元格中.这是我设置单元格的 imageView 的代码:

The image is loaded but it doesn't appear in the cell until I touch (select) that cell. Here is my code that sets the imageView of the cell:

//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]; 
}

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

The above code is part of the:

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

有什么想法吗?

推荐答案

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

I think in this case you're using a iOS defined UITableViewCell (and not a custom one), that is one of those cells that you get from "initWithStyle:reuseIdentifier:". Now these cells are internally built in such way that if you don't assign some content immediately the corresponding subview will not be added in the cell's contentView hierarchy. This kind of behavior is typical of complex custom views, where the final cell layout can be determined only when all its subviews have their content fully specified. (E.g. you have two labels, let's say label A and label B, label A is multirow and you want to add label-B immediately below label-A, then you can place label-B correctly only once you have the label-A content and you calculate its height.). So I suppose the built-in iOS table cells are done in the same way and the subviews hierarchy is built at the "layoutSubviews" stage.

在您的情况下,您创建了单元格,但推迟了提供图像的时间.但是此时 layoutSubviews 已经被调用,并且没有任何图像,甚至没有分配相应的 imageView 并将其添加到 contentView 层次结构中!

In your case you create the cell but defer the time the image is provided. But at this point the layoutSubviews has been called yet and without any image the corresponding imageView is not even allocated and added in the contentView hierarchy!

因此,根据我的说法,您的解决方案是立即为您的资产分配一个占位符"(占位符当然可以是透明图像),这将保证您创建内部 UIImageView.注意图像大小,它应该接近预期的图像大小,原因与上述相同.一旦你的完成块被调用,图像视图应该已经存在并且图像将出现.

So, according to me, the solution for you is just to assign immediately a "placeholder" for your asset (the placeholder can be a transparent image of course) which will guarantee you of the internal UIImageView creation. Be careful with the image size, it should be close to the expected image size, for the same reason explained above. As soon as your finish block is called the image view should be already there and the image will appear.

点击单元格时出现图像的原因是该操作再次调用了layoutSubviews.在这种情况下,整个代码可能会重新执行,并且在此之前您已经调用了setImage:",然后设置了内部image"属性,并且 contentView 层次结构将使用新图像重建.

The reason why when you tap on the cell the image appears, is due to the fact that this operation calls the layoutSubviews again. In this case the whole code is probably re-executed and as you already called "setImage:" before then the internal "image" property is set and the contentView hierarchy will be rebuilt with the new image.

您的问题的另一种可能解决方案当然是使用自定义" UITableViewCell(例如从 Nib 加载).在这种情况下,您的所有子视图都将被加载,您只需使用他的标签访问 UIImageView(阅读苹果文档以了解有关从 Nib 文件创建表格视图单元格的有用技术的更多信息).

Another possible solution to your problem is of course to use a "custom" UITableViewCell (e.g. loaded from a Nib). In such case all your subviews will be loaded and you will simply access to the UIImageView using his tag (read apple docs to know more about this useful technique to create table view cells from a Nib file).

这篇关于UITableView 中的单元格 imageView 直到被选中才会出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆