TableView上的HeaderView放WebView

2018-07-19  本文已影响24人  加菲猫哈哈哈哈

TableView上的HeaderView放WebView

滑动起来没有一丝卡顿现象亲测

{

UIView *view;

UIImageView *image;

CGFloat webViewHeight;

}

- (void)viewDidLoad {

[super viewDidLoad];

self.table=[[UITableView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(self.webView.frame),kScreenWidth,kScreenHeight-self.tabBarController.tabBar.frame.size.height)];

self.table.delegate=self;

self.table.dataSource=self;

self.table.separatorStyle=UITableViewCellSelectionStyleNone;

[self.view addSubview:self.table];

view= [[UIView alloc]initWithFrame:CGRectMake(0,0,kScreenWidth,kScreenHeight)];

self.table.tableHeaderView=view;

_webView= [[UIWebView alloc]initWithFrame:CGRectMake(0,0,kScreenWidth,kScreenHeight)];

[_webView setScalesPageToFit:YES];//大小自适应

_webView.scrollView.scrollEnabled=NO;

[self.webViewloadRequest:[[NSURLRequest alloc]initWithURL:[NSURLURLWithString:@"你的URL"]]];

[view addSubview:_webView];

_webView.delegate=self;

_webView.scrollView.delegate=self;

[_webView.scrollViewaddObserver:self forKeyPath:@"contentSize"options:NSKeyValueObservingOptionNew context:nil];

}

- (void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void*)context

{

if([keyPath isEqualToString:@"contentSize"]) {

webViewHeight= [[_webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] floatValue];

CGRect newFrame=_webView.frame;

newFrame.size.height=webViewHeight;

_webView.frame= newFrame;

[_webView sizeToFit];

CGRectFrame =view.frame;

Frame.size.height= Frame.size.height+_webView.frame.size.height;

view.frame= newFrame;

[self.table setTableHeaderView:view];//这句话才是重点

}

}

- (void)webViewDidFinishLoad:(UIWebView*)webView

{

CGFloat sizeHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"]floatValue];

self.webView.frame=CGRectMake(0,0,kScreenWidth, sizeHeight);

stro.contentSize=CGSizeMake(kScreenWidth, sizeHeight+1);

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

}

#pragma mark返回每组行数

-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{

return5;

}

#pragma mark返回每行的单元格

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{

staticNSString*CellIdentifier =@"Cell";

UITableViewCell*cell = [UITableViewCell dequeueReusableCellWithIdentifier:CellIdentifier];

if(!cell) {

cell = [[commentsTableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifierIndexPa:indexPath];

}

returncell;

}

#pragma mark返回每行高度

- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath

{

return150;

}

上一篇 下一篇

猜你喜欢

热点阅读