简体   繁体   English

UITableView中的cell imageView直到被选中才会出现

[英]cell imageView in UITableView doesn't appear until selected

  1. Here is a video of the problem: http://youtu.be/Jid0PO2HgcU 这是一个问题的视频:http: //youtu.be/Jid0PO2HgcU

  2. I have a problem when trying to set the image for the [cell imageView] in a UITableView from the asset library. 尝试从资产库中为UITableView中的[cell 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: 这是我的代码设置单元格的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]; 
}

The above code is part of the: 以上代码是以下代码的一部分:

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

Any ideas? 有任何想法吗?

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:". 我想在这种情况下你使用iOS定义的UITableViewCell(而不是自定义的),这是你从“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. 现在这些单元格内部构建的方式是,如果您不立即分配某些内容,则不会在单元格的contentView层次结构中添加相应的子视图。 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. 这种行为是复杂自定义视图的典型行为,其中最终单元格布局只有在其所有子视图的内容都已完全指定时才能确定。 (Eg 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.). (例如,你有两个标签,比如标签A和标签B,标签A是多行的,你想在标签A的正下方添加标签B,那么只有在你有标签-A内容后才能正确放置标签-B并计算它的高度。 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. 所以我认为内置的iOS表格单元格以相同的方式完成,子视图层次结构是在“layoutSubviews”阶段构建的。

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! 但此时layoutSubviews已经被调用,没有任何图像,相应的imageView甚至没有被分配并添加到contentView层次结构中!

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. 因此,根据我的说法,您的解决方案就是立即为您的资产分配一个“占位符”(占位符当然可以是透明图像),这将保证您创建内部UIImageView。 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. 调用完成块后,图像视图应该已经存在,图像将会出现。

The reason why when you tap on the cell the image appears, is due to the fact that this operation calls the layoutSubviews again. 当您点击图像出现的单元格时,原因是此操作再次调用layoutSubviews。 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. 在这种情况下,可能会重新执行整个代码,并且在此之前已经调用了“setImage:”,然后设置内部“image”属性,并使用新图像重建contentView层次结构。

Another possible solution to your problem is of course to use a "custom" UITableViewCell (eg loaded from a Nib). 您问题的另一个可能的解决方案当然是使用“自定义”UITableViewCell(例如从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). 在这种情况下,所有子视图都将被加载,您只需使用他的标签访问UIImageView(阅读apple文档以了解更多有关从Nib文件创建表视图单元的有用技术)。

you need to layout cell after image set 你需要在图像集后布局单元格

just like 就像

[cell setNeedsLayout]; [cell setNeedsLayout];

I scratched my head for so long and finally figured it out. 我摸不着头脑,终于想通了。

My mistake was that I was setting image in cell.imageView when I should be setting my actual outlet cell.eventImageView . 我的错误是我在设置我的实际插座cell.eventImageView时在cell.imageView中设置图像。 It was messing with the generic imageview provided in UITableViewCell . 它搞乱了UITableViewCell提供的通用imageview。 Hope it helps somebody. 希望它对某人有帮助。

This is my way how i solved these problem, i know is not the best but that work :) 这是我如何解决这些问题的方式,我知道这不是最好但是工作:)

    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;
    });

In my case I was setting up a prototype cell through a Storyboard, but was accidentally using the dequeueCellWithIdentifier:forIndexPath method instead of dequeueCellWithIdentifier: . 在我的例子中,我是通过Storyboard设置原型单元格,但是偶然使用了dequeueCellWithIdentifier:forIndexPath方法而不是dequeueCellWithIdentifier: .

The first method is only to be used when appropriating cells to a tableview through the programmatic UITableView registerClass:forReuseIdentifier: or registerNib:forReuseIdentifier methods. 第一种方法仅在通过程序化UITableView registerClass:forReuseIdentifier:registerNib:forReuseIdentifier方法将单元格分配到tableview时使用。

I was having a similar problem. 我遇到了类似的问题。 I had registered the default UITableViewCell class instead of my custom class. 我已经注册了默认的UITableViewCell类而不是我的自定义类。

I had this: 我有这个:

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

but I should have had this: 但我应该有这个:

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

The cell creation all looked to be working as I stepped through the debugger, but nothing showed up until I selected each row. 当我逐步调试调试器时,单元格创建看起来都有效,但在我选择每一行之前没有任何内容出现。

Sometime it happens when you are using custom UITableViewCell with labels, images etc but also somewhere you are setting default UILabel of cell. 有时,当您使用带有标签,图像等的自定义UITableViewCell时,您也会在某处设置默认的单元格UILabel。 for example 例如

customCell.textLabel.text = @"some text"

To solve this problem, avoid using default label, instead add your own UILabel in your custom cell. 要解决此问题,请避免使用默认标签,而是在自定义单元格中添加自己的UILabel。

I get same issue when i use this code: 我使用此代码时遇到同样的问题:

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!";
        }

But I fix it by replace: 但我通过替换修复它:

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

To: 至:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM