如何在表单元格中使用UIWebView? [英] How do I use a UIWebView in a Table Cell?

查看:99
本文介绍了如何在表单元格中使用UIWebView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个表格,其中一些文本是HTML,所以我使用UIWebView作为我的自定义表格单元格的子视图。我已经遇到了一个问题 - 当我在表中向下滚动,它需要UIWebViews一秒钟更新。例如,我将在编号为1,2和3的行查看单元格。我向下滚动到说8,9和10.一会儿,在单元格#8中可见的UIWebView的内容是来自小区#1的内容,小区#9中的内容是来自小区#2的内容,等等。

I'm building a table with some text that is HTML, so I am using a UIWebView as a subview of my custom table cells. I already ran into one problem - as I scrolled down in the table, it would take the UIWebViews a second to update. For example, I'd be viewing Cells at rows numbered 1, 2, and 3. I'd scroll down to say 8, 9, and 10. For a moment, the content of the UIWebView that was visible in cell #8 was the content from cell #1, the content in cell #9 was that from cell #2, and so on.

我学到的问题是,UIWebViews只是缓慢地渲染他们的文本。建议改为预先加载内容到UIWebView尽快我可以而不是等待,直到表收到cellForRowAtIndexPath。所以现在,我有一个域对象,之前只有WebView的文本内容 - 但现在它实际上有一个对UIWebView本身的引用。

I learned that the problem was that UIWebViews simply render their text slowly. It was suggested to instead preload the content into the UIWebView as soon as I could instead of waiting until the table receives the cellForRowAtIndexPath. So now, I have a Domain Object which before just had the text content of the WebView - but now it actually has a reference to the UIWebView itself.

但现在UIWebView中的一些内容渲染,当我滚动表时,UIWebView只显示为一个灰色框。如果我触摸灰色框,它实际上会接收到触摸并更新WebView - 例如,如果我触摸一个链接(我可能或不可以做,因为框是灰色的,这将是一个运气)

But now some of the content in the UIWebView renders, and when I scroll through the table the UIWebView shows only as a grey box. If I touch the grey box, it will actually receive the touch and update the WebView - for example if I touch a link (which I may or may not do, since the box is gray and it would be by a stroke of luck), the page that was linked to will be requested and displayed properly.

- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) {
            // I suppose this isn't necessary since I am just getting it from the
            // Domain Object anyway
    content = [[UIWebView alloc] initWithFrame:CGRectZero];
    content.backgroundColor = [UIColor blackColor];
    [self addSubview:content];
    [content release];
}
return self;
}

// called by cellForRowAtIndexPath 
- (void)setMyDomainObject:(MyDomainObject*)anObject {
UIWebView *contentWebView = anObject.contentWebView;
int contentIndex = [self.subviews indexOfObject:content];
[self insertSubview:contentWebView atIndex:contentIndex];
}


推荐答案

将使用几个UILabels,每个具有不同的字体。然后策略性地将它们放置在单元格内,使它们看起来像是连续的文本。我已经看到这在一个UITableView中完成了非常好的结果。

One way to deal with this would be to use several UILabels, each with different fonts. Then strategically place them within the cell as to make them seem like contiguous text. I've seen this done in a UITableView with very good results.

另一个性能问题可能是你覆盖UItableViewCell和它的init方法。这几乎总是导致在UITableView中的性能较差。而只需使用常规的UITableViewCell实例并向其contentView添加子视图。

Another performance problem may be that you are overiding UItableViewCell and it's init method. This almost always leads to poor performance in a UITableView. Instead just use the regular UITableViewCell instances and add subviews to it's contentView.

这已经由Matt Gallagher的轻松自定义UITableView绘图

This has been covered extensively by Matt Gallagher's Easy Custom UITableView Drawing.

描述的另一种方法是使用Three20,它也可以给你样式文本。

Another method described was to use Three20, which can give you styled text as well.

预加载不会有帮助:当UIWebview在屏幕外呈现时,这会减慢UI;你应该总是在iPhone上练习延迟加载。

Preloading won't help: this would slow down the UI when UIWebviews are being rendered offscreen; you should always practice lazy loading on the iPhone.

将UIWebviews放在UITableView中会带来不可接受的性能损失。

Putting UIWebviews in a UITableView will come with a potentially unacceptable performance hit. You will need to use other means to accomplish your goal.

不幸的是,NSAttributedString在UIKit中不可用,这很容易解决你的问题。

Unfortunately, NSAttributedString is unavailable in UIKit, which could easily solve your problem.

这篇关于如何在表单元格中使用UIWebView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆