TableView上的HeaderView放WebView

滑動起來沒有一絲卡頓現象親測web

{this

UIView*view;lua

UIImageView*image;server

CGFloatwebViewHeight;ip

}string

- (void)viewDidLoad {it

[superviewDidLoad];io

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

self.table.delegate=self;object

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];//大小自適應

_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];//這句話纔是重點

}

}

- (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返回每組行數

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

}

做者:iOS我稱王 連接:http://www.jianshu.com/p/66b367b8b3ff 來源:簡書 著做權歸做者全部。商業轉載請聯繫做者得到受權,非商業轉載請註明出處。
相關文章
相關標籤/搜索