TableView上的HeaderView放WebView

###滑動起來沒有一絲卡頓現(xiàn)象親測

```

{

UIView*view;

UIImageView*image;

CGFloatwebViewHeight;

}

- (void)viewDidLoad {

[superviewDidLoad];

self.table=[[UITableViewalloc]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.viewaddSubview:self.table];

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

self.table.tableHeaderView=view;

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

[_webViewsetScalesPageToFit:YES];//大小自適應(yīng)

_webView.scrollView.scrollEnabled=NO;

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

[viewaddSubview:_webView];

_webView.delegate=self;

_webView.scrollView.delegate=self;

[_webView.scrollViewaddObserver:selfforKeyPath:@"contentSize"options:NSKeyValueObservingOptionNewcontext:nil];

}

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

{

if([keyPathisEqualToString:@"contentSize"]) {

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

CGRectnewFrame=_webView.frame;

newFrame.size.height=webViewHeight;

_webView.frame= newFrame;

[_webViewsizeToFit];

CGRectFrame =view.frame;

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

view.frame= newFrame;

[self.table setTableHeaderView:view];//這句話才是重點(diǎn)

}

}

- (void)webViewDidFinishLoad:(UIWebView*)webView

{

CGFloatsizeHeight = [[webViewstringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"]floatValue];

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

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

}

- (void)didReceiveMemoryWarning {

[superdidReceiveMemoryWarning];

}

#pragma mark返回每組行數(shù)

-(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;

}

```

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容