如何使用DTCoreText在UITableViewCell中显示图像 [英] How to show image in UITableViewCell with DTCoreText
问题描述
我在UITableViewCell中有DTAttributedTextContentView
,并尝试使用html(带有图像)加载它,但是找不到合适的方法来执行此操作.我查看了演示中的DemoTextViewController.m
,该图像使用
I have DTAttributedTextContentView
in UITableViewCell and try to load it with html (with image), but can't find a proper way to do this. I have look into DemoTextViewController.m
in Demo which have image load with
- (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
But I don't know how this will apply to UITableViewCell I tried
- (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
的大小.我找不到任何有关如何执行此操作的文档.
But the cell height not show correctly and the image isn't resize to fit DTAttributedTextContentView
size. I can't find any document of how to implement this.
如果您有更好的选择或解决方案,请告诉我.
If you have a better choice or solution, please tell me.
推荐答案
我也很难使它起作用,有关此的信息不多,但是我终于使它起作用了.
当您创建DTAttributedTextCell
时,将其DTAttributedTextContentView
属性的委托设置为tableviewcontroller
I had difficulty getting this to work too, not much info around on this but I finally got it working.
When you make your DTAttributedTextCell
set the delegate of it's DTAttributedTextContentView
property to your 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
时使用.
From inside this method create your DTLazyImageView
and set its delegate. I made a subclass of DTLazyImageView
to hold an extra property, a reference to the DTAttributedTextContentView
of that individual cell, to be used when the DTLazyImageViewDelegate
is called.
然后在调用- (void)lazyImageView:(DTLazyImageView *)lazyImageView didChangeImageSize:(CGSize)size
时,将传递的lazyImageView
变量转换为您的子类以获取存储的DTAttributedTextContentView
.使用示例中提供的相同方法,只是将_textView.attributedTextContentView
替换为您继承的DTAttributedTextContentView
.
Then when - (void)lazyImageView:(DTLazyImageView *)lazyImageView didChangeImageSize:(CGSize)size
is called take the lazyImageView
variable that was passed and cast it to your subclass to get the DTAttributedTextContentView
you stored. Use the same method that comes in the example but just replace the _textView.attributedTextContentView
with the DTAttributedTextContentView
you carried over.
还请确保像在DemoSnippetsViewController中一样制作单元,如果要加载图像,则需要具有可变高度的单元,并且必须实现- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
Also be sure to make the cells the way he does in the DemoSnippetsViewController, if you're loading images you will need to have variable height cells and you must implement - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
我大约一年都没有碰过我的代码,所以我对此不太确定.但是这是我认为正在发生的事情.我从不调用- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
,我只是实现它,并且在重新加载单元格时调用它.什么时候重新加载?从我收集到的信息中,我实现了懒惰图像视图- (void)lazyImageView:(DTLazyImageView *)lazyImageView didChangeImageSize:(CGSize)size;
的委托,因此,从网络上下载图像后,在该调用中,我检查图像大小是否已更改,然后调用[DTAttributedTextContentView relayoutText]
,我认为启动了重新加载.
I haven't touched my code in about a year so I'm not so sure about this. But here is what I think is happening. I never call - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
, I just implement it and it is called when the cell is reloaded. When is it reloaded? From what i gather, I implement the delegate for the lazy image view - (void)lazyImageView:(DTLazyImageView *)lazyImageView didChangeImageSize:(CGSize)size;
So after the image is downloaded from the web, in that call I check if the image size has changed and call [DTAttributedTextContentView relayoutText]
, which I think kicks off the reloading.
这是我的heightForRowAtIndexPath实现:
Here's my heightForRowAtIndexPath implementation:
- (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:]
来传递带有小缓冲区的新大小.
Once the download is complete and the refresh is called it calls the [DTAttributedTextCell requiredRowHeightInTableView:]
to pass along the new size with a small buffer.
这篇关于如何使用DTCoreText在UITableViewCell中显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!