0

DTAttributedTextContentView在 UITableViewCell 中并尝试使用 html(带图像)加载它,但找不到合适的方法来执行此操作。我DemoTextViewController.m在 Demo 中查看了具有图像加载的

- (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大小。我找不到任何关于如何实现这一点的文档。

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

4

1 回答 1

3

我也很难让它工作,关于这个的信息不多,但我终于让它工作了。当您将DTAttributedTextCellset 的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:]以传递新大小和一个小缓冲区。

于 2013-05-08T17:50:50.157 回答