How do I use a UIWebView in a Table Cell?(如何在表格单元格中使用 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.
我了解到问题在于 UIWebView 只是缓慢地呈现它们的文本.建议尽快将内容预加载到 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];
}
推荐答案
解决这个问题的一种方法是使用多个 UILabel,每个 UILabel 都有不同的字体.然后策略性地将它们放置在单元格中,使它们看起来像连续的文本.我已经在 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 的 Easy Custom UITableView Drawing一个>.
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 完成.
What you are attempting currently can't be done with UIWebview.
预加载无济于事:当 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在表格单元格中使用 UIWebView?
基础教程推荐
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01