UITableVIewCell中webView设置高度问题
来源:互联网 发布:三国群英传2 mac版本 编辑:程序博客网 时间:2024/06/06 00:31
若要在cell中显示webview,必须先知道webview的高度,才能给cell的代理方法赋值,关键就这一句话,通过监听实现获取webview高度,啥也不说了,直接上代码。
这个是cell里的.m文件的代码
#import "InformationDetailCell.h"
#import "WebView.h"
@interface InformationDetailCell()<UIWebViewDelegate>
@end
@implementation InformationDetailCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if (self = [superinitWithStyle:style reuseIdentifier:reuseIdentifier]) {
self.accessoryType =UITableViewCellAccessoryNone;
[selfcreateUI];
}
returnself;
}
-(void)createUI{
self.web= [[UIWebViewalloc]initWithFrame:CGRectMake(0,0, self.frame.size.width,self.frame.size.height)];
self.web.scrollView.scrollEnabled = NO;
[self.websizeToFit];
[self.contentViewaddSubview:self.web];
self.web.delegate =self;
}
-(void)setModel:(InformationDetailDataModel *)model{
_model = model;
[self.webloadHTMLString:self.model.contentbaseURL:nil];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
//字体颜色
[self.webstringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'white'"];
//页面背景色
[self.webstringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#18171D'"];
//获取到webview的高度
CGFloat height = [[webViewstringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"]floatValue];
webView.frame=CGRectMake(0,0, self.frame.size.width,height);
[webView sizeToFit];
NSDictionary *dic = [NSDictionarydictionaryWithObject:[NSStringstringWithFormat:@"%f",height]forKey:@"frame"];
[[NSNotificationCenterdefaultCenter] postNotificationName:@"ChangeCellHeight"object:niluserInfo:dic];
}
@end
然后是控制器里的代码
#import "InformationDetailVC.h"
#import "InformationDetailCell.h"
@interface InformationDetailVC ()
{
CGFloat cellHeight;
BOOL isLoad;
int loadNumber;
}
@property (nonatomic,strong)InformationDetailDataModel *dataModel;
@end
@implementation InformationDetailVC
- (void)viewDidLoad {
[superviewDidLoad];
loadNumber =0;
[selfconfigTableView];
[[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(ChangeCellHeight:)name:@"ChangeCellHeight"object:nil];
}
-(void)ChangeCellHeight:(NSNotification *)noti{
//使用userInfo处理消息
NSDictionary *dic = [notiuserInfo];
NSString *info = [dicobjectForKey:@"frame"];
cellHeight = [infofloatValue];
if (!isLoad) {
[self.tableViewreloadData];
if (loadNumber ==1) {
isLoad =YES;
dispatch_time_t delayTime =dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3/*延迟执行时间*/ * NSEC_PER_SEC));
dispatch_after(delayTime,dispatch_get_main_queue(), ^{
[self.tableViewreloadData];
self.tableView.alpha =1;
});
}
loadNumber +=1;
}
}
-(void)configTableView{
WeakSelf
self.headerRefresh = ^{
[weakSelf loadData];
};
}
-(void)loadData{
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (section ==0) {
return1;
}else{
returnself.dataModel.snapList.count;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section ==0) {
if (cellHeight ==0) {
return0.1;
}else{
returncellHeight + 16;
}
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
InformationDetailCell *cell = [tableViewdequeueReusableCellWithIdentifier:@"InformationDetailCell"];
if (cell ==nil) {
cell = [[InformationDetailCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:@"InformationDetailCell"];
}
[cell setModel:self.dataModel];
return cell;
}
@end
- UITableVIewCell中webView设置高度问题
- 设置UITableViewCell高度的问题
- 详解Autolayout 设置UITableViewCell 高度问题
- UITableViewCell动态高度设置
- 如何设置UITableViewCell中cell内容的自适应高度
- 动态设置UITableViewCell的高度
- 放在UITableVIewCell上的webView的高度自适应的问题
- WebView设置最大高度
- UITableViewCell 的一些设置(多行,高度)
- IOS下动态设置UITableViewCell的高度
- android WebView设置最大高度
- android WebView设置最大高度
- android 设置webview的高度,解决网页高度过小头部空白问题
- webview显示html代码,并设置html中字体的大小,获得webview高度
- 关于(UITableViewcell)contentView高度的问题
- iOS UITableViewCell自适应高度单元格问题?
- 在程序运行中调整UITableViewCell高度
- 获取WebView高度的问题
- Node.js 实现爬虫(3) —— 保存爬取的数据
- 打造平台产品的经验----《启示录》
- 如何利用Windows自带calc计算机进行取模运算
- MySQL--操作简记(汇总数据(聚集函数),分组数据,子查询)
- eclipse配置黑色主题
- UITableVIewCell中webView设置高度问题
- Kafka异常处理(客户端和消费者都找不到topic)
- springmvc初始化过程
- EHCache does not allow attribute "maxEntriesLocalHeap".
- Workerman学习之路
- 剑指offer(1):二维数组中的查找
- AsyncTask多条目加载
- 使用JasperReport和poi导出Excel
- CentOS6设置静态IP而且还可以上网