UITableViewCell嵌套UIWebView, 解决WebView加载本地Html文件高度获取不准确问题,支持复杂图文
PackageNameBusinessDescriptionCell.h文件
#import <UIKit/UIKit.h>
typedef void(^DescriptionBlock)(void);
@interface PackageNameBusinessDescriptionCell : UITableViewCell
@property (nonatomic , strong) NSString *desStr; //赋值的Html文本
@property (nonatomic , assign) CGFloat cellHeight; //需要返回给TableView的
@property (nonatomic , copy) DescriptionBlock myBlock; //Block回调
@end
PackageNameBusinessDescriptionCell.m文件
#import "PackageNameBusinessDescriptionCell.h"
@interface PackageNameBusinessDescriptionCell()<UIWebViewDelegate>
@end
@implementation PackageNameBusinessDescriptionCell
- (void)webViewDidFinishLoad:(UIWebView *)webView {
//加载完成后获取WebView实际高度
CGFloat webViewHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] floatValue];
webView.frame = CGRectMake(0, 0, SCREEN_WIDTH, webViewHeight);
//赋值并回调需要的cellHeight
self.cellHeight = webViewHeight;
if (self.myBlock) {
self.myBlock();
}
}
-(void)setDesStr:(NSString *)desStr {
_desStr = desStr;
//移除所有视图
[self.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
//添加WebView 注:这里的Frame高度必须赋值
UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 1)];
webView.scrollView.scrollEnabled = NO;
webView.delegate =self;
[webView sizeToFit];
[self.contentView addSubview:webView];
//加载Html文件
[webView loadHTMLString:desStr baseURL:nil];
}
@end
本地html文件:
_businessInfo = @"<p>【资费标准】<br />1、套餐包含30分钟国内通话、100MB国内流量、来电显示,流量执行不清零规则,国内被叫免费(不含港澳台)。<br />2、超出套餐包含通话时长后,长市漫一口价0.19元/分钟,不享受4G飞享套餐专属3元语音包。<br />3、4G飞享套餐默认开通流量安心包,即超出套餐包含流量后,先按0.29元/M计费,使用约34M赠送66M,即100M以内最多收取10元;累计6次之后,赠送400M,可以免费使用至1G,即1G以内最多收取60元。超出1G之后,按上述计费方式,以此类推。<br />4、短信费:发送国内短信0.1元/条,发送国内彩信0.5元/条。<br />5、客户变更套餐后,原套餐内剩余流量不享受流量不清零服务。<br />6、套餐变更均是次月生效,每月仅允许一次变更套餐请求,且不允许取消。<br />注:若您存在最低协议消费,建议您到营业厅更改套餐,避免产生补收费。</p>";
ViewController中TableView调用
@property (nonatomic, strong) PackageNameBusinessDescriptionCell *businessDescriptionCell;
@property (nonatomic , strong) NSString *businessInf; //本地Html
//获取数据后赋值
self.businessDescriptionCell.desStr = self.packModel.businessInfo;
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = self.businessDescriptionCell;
return cell;
}
//返回Cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return self.businessDescriptionCell.cellHeight;
}
//LazyLoad
- (PackageNameBusinessDescriptionCell *)businessDescriptionCell{
if (!_businessDescriptionCell) {
NSString *cellIdentifier = @"PackNameBusinessDescripCelIden";
_businessDescriptionCell = [_tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!_businessDescriptionCell) {
_businessDescriptionCell = [[PackageNameBusinessDescriptionCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
_businessDescriptionCell.selectionStyle = UITableViewCellSelectionStyleNone;
}
//回调刷新Cell内容及高度
@VPWeakObj(self);
_businessDescriptionCell.myBlock = ^{
@VPStrongObj(self);
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:2] withRowAnimation:UITableViewRowAnimationNone];
};
}
return _businessDescriptionCell;
}
网友评论