繁体   English   中英

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

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

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

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

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

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

因此,根据我的说法,您的解决方案就是立即为您的资产分配一个“占位符”(占位符当然可以是透明图像),这将保证您创建内部UIImageView。 小心图像大小,它应该接近预期的图像大小,原因与上面解释的相同。 调用完成块后,图像视图应该已经存在,图像将会出现。

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

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

你需要在图像集后布局单元格

就像

[cell setNeedsLayout];

我摸不着头脑,终于想通了。

我的错误是我在设置我的实际插座cell.eventImageView时在cell.imageView中设置图像。 它搞乱了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时使用。

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

我有这个:

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

但我应该有这个:

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

当我逐步调试调试器时,单元格创建看起来都有效,但在我选择每一行之前没有任何内容出现。

有时,当您使用带有标签,图像等的自定义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!";
        }

但我通过替换修复它:

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

至:

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

暂无
暂无

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

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