具有动态高度的表视图单元格

时间:2015-05-17 15:37:14

标签: ios uitableview

我希望你能帮助我解决这个问题。

我有一个表视图,单元格有一个图像,单元格的高度取决于图像。我使用SDWebImage来管理图像的下载。为了获得图像,我在cellForRowAtIndexPath中使用了这句话:

[cell.imageView func_crossdissolveFromURL:imageURL];

函数func_crossdissolveFromURL如下:

- (void)func_crossdissolveFromURL:(NSString *)urlString {
    [self.activityIndicator stopAnimating];
    [self.downloadOperation cancel];

    if (!urlString) {
        return;
    }

    UIImage *memImage = [[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:urlString];
    if (memImage) {
        [self _crossdisolve:memImage cacheType:SDImageCacheTypeMemory];
        return;
    }

    [self.activityIndicator startAnimating];
    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    __weak UIImageView *wself = self;
    id<SDWebImageOperation> operation = [manager downloadWithURL:[NSURL URLWithString:urlString]
                                                     options:SDWebImageAllowInvalidSSLCertificates
                                                    progress:nil
                                                   completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) {
                                                       [self.activityIndicator stopAnimating];
                                                       if (!wself) return;
                                                       dispatch_main_sync_safe(^{
                                                           if (!wself || !image) return;
                                                           [self _crossdisolve:image cacheType:cacheType];
                                                       });
                                                   }];
    self.downloadOperation = operation;
}

调用_crossdissolve的函数接近结尾是这样的:

- (void)_crossdisolve:(UIImage *)image cacheType:(SDImageCacheType)cacheType {
    CGFloat duration = (cacheType == SDImageCacheTypeMemory) ? 0 : 0.25f;
    [UIView transitionWithView:self
                  duration:duration
                   options:UIViewAnimationOptionTransitionCrossDissolve
                animations:^{
                    self.image = image;
                }
                completion:^(BOOL finished) {
                    [self setNeedsLayout];
                }];
}

那么,我怎么能让每个单元格都具有内容图像的高度?提前谢谢。

1 个答案:

答案 0 :(得分:1)

你需要两件事:

  1. 将单元格高度设置为自动:self.tableView.rowHeight = UITableViewAutomaticDimension;
  2. 使用自动布局
  3. 定义单元格的高度

    要在显示单元格后更改单元格的高度,可以重新加载单元格。