如何显示的UITableViewCell与DTCoreText图像(How to show imag

2019-08-08 06:15发布

我有DTAttributedTextContentView在UITableViewCell中,并尝试用HTML(含图片)来加载它,但无法找到一个合适的方式来做到这一点。 我有考虑DemoTextViewController.m在演示具有图像负载

- (void)lazyImageView:(DTLazyImageView *)lazyImageView didChangeImageSize:(CGSize)size {
    NSURL *url = lazyImageView.url;
    CGSize imageSize = size;

    NSPredicate *pred = [NSPredicate predicateWithFormat:@"contentURL == %@", url];

    // update all attachments that matchin this URL (possibly multiple images with same size)
    for (DTTextAttachment *oneAttachment in [_textView.attributedTextContentView.layoutFrame textAttachmentsWithPredicate:pred])
    {
        oneAttachment.originalSize = imageSize;

        if (!CGSizeEqualToSize(imageSize, oneAttachment.displaySize))
        {
            oneAttachment.displaySize = imageSize;
        }
    }

    // redo layout
    // here we're layouting the entire string, might be more efficient to only relayout the paragraphs that contain these attachments
    [_textView.attributedTextContentView relayoutText];
}

但我不知道这将如何应用到的UITableViewCell我试着

- (void)lazyImageView:(DTLazyImageView *)lazyImageView didChangeImageSize:(CGSize)size {
    NSURL *url = lazyImageView.url;
    CGSize imageSize = size;

    NSPredicate *pred = [NSPredicate predicateWithFormat:@"contentURL == %@", url];

    for (UITableViewCell *cell in self.tableView.visibleCells) {
        if ([cell isKindOfClass:[CommentCell class]]) {
            CommentCell *cc = (CommentCell *)cell;
            for (DTTextAttachment *oneAttachment in [cc.attributedTextContentView.layoutFrame textAttachmentsWithPredicate:pred])
            {
                oneAttachment.originalSize = imageSize;

                if (!CGSizeEqualToSize(imageSize, oneAttachment.displaySize))
                {
                    oneAttachment.displaySize = CGSizeMake(300, 100);
                }
            }
            [cc.attributedTextContentView relayoutText];
        }

    }

}

但是,单元格的高度无法正确显示和图像不调整大小以适应DTAttributedTextContentView大小。 我找不到如何实现此的任何文件。

如果你有更好的选择或解决方案,请告诉我。

Answer 1:

我难以得到这个工作也没有太大的这个周围的信息,但我终于得到了它的工作。 当您做您DTAttributedTextCell设置它的代表是DTAttributedTextContentView属性为您tableviewcontroller

cell.attributedTextContextView.delegate = self;

而实现此方法:

- (UIView *)attributedTextContentView:(DTAttributedTextContentView *)attributedTextContentView viewForAttachment:(DTTextAttachment *)attachment frame:    (CGRect)frame{

    if([attachment isKindOfClass:[DTImageTextAttachment class]]){

       FVLazyImageView *imageView = [[FVLazyImageView alloc] initWithFrame:frame];
       imageView.contextView = attributedTextContentView;
       imageView.delegate = self;

       // url for deferred loading
       imageView.url = attachment.contentURL;
       return imageView;
   }
    return nil;
}

从这个方法中创建您的DTLazyImageView并设置其委托。 我做的一个子类DTLazyImageView持有额外的属性,在参考DTAttributedTextContentView个别细胞的,当要使用DTLazyImageViewDelegate被调用。

然后,当- (void)lazyImageView:(DTLazyImageView *)lazyImageView didChangeImageSize:(CGSize)size被称为拿lazyImageView已传递变量,并将其投放到您的子类,以获得DTAttributedTextContentView你存储。 使用自带的例子同样的方法,但只需更换_textView.attributedTextContentViewDTAttributedTextContentView你结转。

此外,一定要使细胞他确实在DemoSnippetsViewController的方式,如果你加载图像,你需要有高度可变的细胞,就必须实现- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

更新Stricklands问题

所以我不太确定这个我没碰过在大约一年我的代码。 但这里是什么,我认为正在发生的事情。 我从来不叫- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath ,我只是实现它,当小区重新加载它被调用。 当它重新加载? 从我收集,我实现懒惰的图像视图的委托- (void)lazyImageView:(DTLazyImageView *)lazyImageView didChangeImageSize:(CGSize)size; 图像从网上下载的操作后,在呼叫我检查图像的大小发生了变化,并调用[DTAttributedTextContentView relayoutText]我想离开重装踢。

这是我的heightForRowAtIndexPath实现:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    DTAttributedTextCell *cell = (DTAttributedTextCell *)[self tableView:tableView preparedCellForIndexPath:indexPath];
    if([indexPath isEqual:self.selectedIndex]){
        return MAX([cell requiredRowHeightInTableView:tableView] + 40,120);
    }
    return MAX([cell requiredRowHeightInTableView:tableView], 80);
}

一旦下载完成,并刷新被称为它调用[DTAttributedTextCell requiredRowHeightInTableView:]沿着与小缓冲区的新大小通过。



文章来源: How to show image in UITableViewCell with DTCoreText