UIWebView in custom UITableViewCell
-
10-07-2019 - |
Question
i am not sure if this is a good route to go,...i have some data i receive from the web which in turn, populates a table view. The problem is, the text is html (p tags, etc). My first thought was to create a uiwebview in the cell, and populate with loadHTMLString. Fact is , it KINDA works. But, i then the cell no longer was the recipient of touches.
SO, before we get too deep in code, is there a better way to populate the cells, than using a UIWebView. It feels like a hack, and i fear even if it works, apple would turn it away.
//from my custom UITableViewCell class:
- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) {
[self setFrame:frame];
webcell = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,frame.size.width-20,frame.size.height)];
[self.contentView addSubview:webcell];
//bloack the webview from touches
UIView *cover = [[UIView alloc] initWithFrame:webcell.frame];
[self.contentView addSubview:cover];
[cover release];
}
return self;
}
-(void)setLabelData:(FeedItem *)feedItem { link = feedItem.link;
NSMutableString *htmlstring = [NSMutableString string];
[htmlstring appendString:@"<html><head><link rel='stylesheet' href='style.css'/></head><body>"];
[htmlstring appendFormat:@"<p>%@</p>",feedItem.title];
[htmlstring appendFormat:@"<p>%@</p>",feedItem.description];
[htmlstring appendString:@"</body></html>"];
[webcell loadHTMLString:htmlstring baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]bundlePath]]];
}
thanks
Solution
unless someone out there has rebuttle, i have concluded that a UIWebView in a UITableViewCell is not a good idea. Albeit, i have not tested with a local htmlString...
OTHER TIPS
We've had some success using webviews in cells. If you want the cell to receive touch events, you need to set the webview's userInteractionEnabled
property to NO. You also need to use a different reuseIdentifier for each cell, since the webview takes some time to load its contents and may get reused before it's ready.
I have done this by statically allocating the webview-cells as soon as I know how many I will need for the table, and start them all loading off-screen. No reuse identifiers, you need to keep track of the cells and where they go yourself.