实现效果如下:

忽略底部的评论视图,太丑了,待完善......

实现思路:

1>页面布局采用TableView实现,顶部"关注"模块的View是TableView的tableHeaderView;

2>采用两个cell,webViewCell和CommentCell,indexPath.row == 0使用webViewCell,其余使用CommentCell;

3>在webViewCell的webView的代理方法中webViewDidFinishLoad计算webView内容的实际高度,block回调控制器刷新indexPath.row == 0的cell即可.

4>tableView代理返回cell直接返回cell里计算的cell.

代码实现如下:

WebViewCell:

#import <UIKit/UIKit.h>
typedef void (^ReloadBlock)();
@interface WebviewCell : UITableViewCell @property(nonatomic, copy) NSString *htmlString;
@property(nonatomic, copy) ReloadBlock reloadBlock; +(CGFloat)cellHeight; @end
#import "WebviewCell.h"
@interface WebviewCell()<UIWebViewDelegate> @property(nonatomic,strong)UIWebView *webview; @end
static CGFloat staticheight = ; @implementation WebviewCell +(CGFloat)cellHeight
{
return staticheight;
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle: style reuseIdentifier:reuseIdentifier]) { // self.webview.scrollView.backgroundColor =[UIColor redColor];
[self.contentView addSubview:self.webview];
}
return self; }
-(void)setHtmlString:(NSString *)htmlString
{
_htmlString = htmlString; self.webview.delegate = self;
// 否则导致网页下方留出了多余的空白
// 手动改变图片适配问题,拼接html代码后,再加载html代码
NSString *myStr = [NSString stringWithFormat:@"<head><style>img{max- width:%f !important;}</style></head>", [UIScreen mainScreen].bounds.size.width - ];
NSString *str = [NSString stringWithFormat:@"%@%@",myStr, htmlString];
[self.webview loadHTMLString:str baseURL:nil];
} -(void)webViewDidFinishLoad:(UIWebView *)webView
{
CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue]+ ;
self.webview.frame = CGRectMake(, , K_SCREEN_WIDTH, height);
self.webview.hidden = NO;
if (staticheight != height+) {
staticheight = height+;
if (staticheight > ) {
if (_reloadBlock) {
_reloadBlock();
}
}
}
} -(UIWebView *)webview { if (!_webview) {
_webview =[[UIWebView alloc]initWithFrame:CGRectMake(, , K_SCREEN_WIDTH, )];
_webview.userInteractionEnabled = NO;
_webview.hidden = YES;
}
return _webview;
}
@end

评论cell和tableHeaderView这里就不贴代码了,很简单的....

控制器代码: 这里都贴上了...自己找重点哟!!!

#import "NewDetailViewController.h"
#import "NewDetailHeaderView.h"
#import "CommentBottomView.h"
#import "HTTPTool.h"
#import "HDNewsDetailModel.h"
#import "News.h"
#import "UIColor+HexColor.h"
#import "NewDetailCommentModel.h"
#import "WebviewCell.h"
#import "DetailCommentCell.h" @interface NewDetailViewController ()<UITableViewDelegate, UITableViewDataSource, UIWebViewDelegate> @property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) NewDetailHeaderView *headerView;
@property (nonatomic, strong) CommentBottomView *bottomView;
@property (nonatomic, assign) CGFloat keyBoardHeight;
@property (nonatomic, assign) CGFloat currentTextViewHeight;
@property (nonatomic,strong) NSMutableArray *array;
@property (nonatomic, strong) NSArray *commentArray; // 评论model数组
@property (nonatomic, strong) UITableView *commnetTableView;
@property (nonatomic, strong) HDNewsDetailModel *detailModel; @end @implementation NewDetailViewController - (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"新闻详情页";
self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor], NSFontAttributeName : [UIFont boldSystemFontOfSize:]};
self.navigationController.navigationBar.barTintColor = [UIColor colorWithHex:@"#4bb6ac"];
self.automaticallyAdjustsScrollViewInsets = NO;
self.navigationController.navigationBar.translucent = NO;
self.currentTextViewHeight = ;
[self addNotification];
[self setupHeaderView];
[self setupBottomView];
[self setupUI];
[self addNavShareItem];
[self requestData];
[self requestCommentData];
} - (void)addNavShareItem {
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"1x_fenxiang"] style:UIBarButtonItemStylePlain target:self action:@selector(shareAction:)];
self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
} #pragma mark - 键盘通知
- (void)addNotification { // 监听键盘的弹出
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeHeight:) name:@"changeHeight" object:nil]; } - (void)keyboardWillShow:(NSNotification *) notification { float animationDuration = [[[notification userInfo] valueForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
CGFloat height = [[[notification userInfo]objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size.height;
self.keyBoardHeight = height;
// 没有弹出键盘 使用这种动画比较顺畅一点
[UIView animateWithDuration:animationDuration animations:^{
CGRect bottomBarFrame = self.bottomView.frame;
bottomBarFrame.origin.y = [UIScreen mainScreen].bounds.size.height - height - self.currentTextViewHeight - ;
// CGRect rc = [self.view convertRect: self.bottomView.frame toView:self.view];
[self.bottomView setFrame:bottomBarFrame];
// 这里注意: 需要把底部view移动到最前面显示
[self.view bringSubviewToFront:self.bottomView];
}];
} - (void)keyboardWillHide:(NSNotification *) notification {
float animationDuration = [[[notification userInfo] valueForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
CGFloat height = [[[notification userInfo]objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
self.keyBoardHeight = height;
[UIView animateWithDuration:animationDuration animations:^{
CGRect bottomBarFrame = self.bottomView.frame;
bottomBarFrame.origin.y = self.view.bounds.size.height - self.currentTextViewHeight;
[self.bottomView setFrame:bottomBarFrame];
}];
} - (void)changeHeight: (NSNotification *)notifi {
CGFloat height = [notifi.userInfo[@"height"] floatValue];
if (height > ) { self.currentTextViewHeight = height;
}
NSLog(@"height --- %f",height);
[UIView animateWithDuration:0.2 animations:^{
CGRect bottomBarFrame = self.bottomView.frame;
bottomBarFrame.origin.y = self.view.bounds.size.height - height - self.keyBoardHeight;
bottomBarFrame.size.height = height + ;
[self.bottomView setFrame:bottomBarFrame];
}];
} #pragma mark - 设置界面 - (void)setupUI {
[self.view addSubview:self.commnetTableView];
self.commnetTableView.frame = CGRectMake(, , K_SCREEN_WIDTH, K_SCREEN_HEIGHT - - );
} - (void)setupHeaderView { self.headerView = [[NewDetailHeaderView alloc]initWithFrame:CGRectMake(, , [UIScreen mainScreen].bounds.size.width, )];
self.headerView.focusButtonBlock = ^(BOOL flag) {
NSLog(@"flag -- %d",flag);
};
[self.commnetTableView setTableHeaderView:self.headerView];
} - (void)setupBottomView {
self.bottomView = [[CommentBottomView alloc]initWithFrame:CGRectMake(, [UIScreen mainScreen].bounds.size.height - - , [UIScreen mainScreen].bounds.size.width, )];
[self.view addSubview:self.bottomView];
} #pragma mark - 数据请求 - (void)requestData {
NSString *url = [NSString stringWithFormat:@"http://huidu.zhonghuilv.net/Index/newslist?newsid=%@",@(self.model.id)];
[HTTPTool postWithURL:url headers:nil params:nil success:^(id json) {
NSLog(@"json = %@",json);
self.detailModel = [[HDNewsDetailModel alloc]init];
[self.detailModel setValuesForKeysWithDictionary:json];
// self.headerView.model = self.detailModel;
[self.headerView configureHeaderAvaterWithImage:json[@"thumb"] title:json[@"title"]];
} failure:^(NSError *error) {
[CombancHUD showInfoWithStatus:@"加载失败"];
}];
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.commentArray.count + ;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row == ) {
WebviewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WebviewCell" forIndexPath:indexPath];
cell.htmlString = self.detailModel.content;
__weak NewDetailViewController *weakSelf = self;
cell.reloadBlock =^()
{ // 刷新webViewCell
[weakSelf.commnetTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
};
return cell;
}
DetailCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CommentCell" forIndexPath:indexPath];
cell.model = self.commentArray[indexPath.row - ];
return cell;
} - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row == ) {
NSLog(@"cellHeight---%f",[WebviewCell cellHeight]);
return [WebviewCell cellHeight];
} else {
return ;
}
} - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return 0.001f;
} - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 0.001f;
} - (void)requestCommentData {
// index/Articlepl
NSString *url = [NSString stringWithFormat:@"http://huidu.zhonghuilv.net/index/Articlepl?newsid=%@",@(self.model.id)];
[HTTPTool postWithURL:url headers:nil params:nil success:^(id json) {
NSLog(@"json = %@",json);
self.commentArray = [NewDetailCommentModel mj_objectArrayWithKeyValuesArray:json];
[self.commnetTableView reloadData];
} failure:^(NSError *error) {
[CombancHUD showInfoWithStatus:@"加载失败"];
}];
} #pragma mark - Private Method - (void)shareAction: (UIBarButtonItem *)item {
NSLog(@"分享");
} - (UITableView *)commnetTableView {
if (!_commnetTableView) {
_commnetTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
_commnetTableView.delegate = self;
_commnetTableView.dataSource = self;
[_commnetTableView registerNib:[UINib nibWithNibName:@"DetailCommentCell" bundle:nil] forCellReuseIdentifier:@"CommentCell"];
[_commnetTableView registerClass:[WebviewCell class] forCellReuseIdentifier:@"WebviewCell"];
_commnetTableView.tableHeaderView = [UIView new];
_commnetTableView.tableFooterView = [UIView new];
}
return _commnetTableView;
} @end

iOS实现页面既显示WebView,WebView下显示TableView,动态计算WebView内容高度的更多相关文章

  1. iOS View自定义窍门——UIButton实现上显示图片,下显示文字

    “UIButton实现上显示图片,下显示文字”这个需求相信大家在开发中都或多或少会遇见.比如自定义分享View的时候.当然,也可以封装一个item,上边imageView,下边一个label.但是既然 ...

  2. iOS 动态计算文本内容的高度

    关于ios 下动态计算文本内容的高度,经过查阅和网上搜素,现在看到的有以下几种方法: 1. //  获取字符串的大小  ios6 - (CGSize)getStringRect_:(NSString* ...

  3. nodejs之获取客户端真实的ip地址+动态页面中引用静态路径下的文件及图片等内容

    1.nodejs获取客户端真实的IP地址: 在一般的管理网站中,尝尝会需要将用户的一些操作记录下来,并记住是哪个用户进行操作的,这时需要用户的ip地址,但是往往当这些应用部署在服务器上后,都使用了ng ...

  4. iOS开发总结-UITableView 自定义cell和动态计算cell的高度

    UITableView cell自定义头文件:shopCell.h#import <UIKit/UIKit.h>@interface shopCell : UITableViewCell@ ...

  5. iOS学习之根据文本内容动态计算文本框高度的步骤

    在视图加载的过程中,是先计算出frame,再根据frame加载视图的,所以在设计计算高度的方法的时候,设计成加号方法; //首先给外界提供计算cell高度的方法 + (CGFloat)heightFo ...

  6. iOS问题处理:如何在Mac下显示Finder中的所有文件

    摘自:http://www.cnblogs.com/elfsundae/archive/2010/11/30/1892544.html 在Unix下工作,你可能需要处理一些“特殊“文件或文件夹,例如/ ...

  7. Ionic+AngularJS 开发的页面在微信公众号下显示不出来原因查究

    ionic 页面 微信浏览器遇到的坑 公司的微信公众号一部分页面是用AngularJS+Ioinc开发,发现在本地浏览器测试的时候都没问题,传到服务器在微信公众号下跑就出问题来,经查是: index- ...

  8. iOS之动态计算文字的高度

    + (CGSize)boundingALLRectWithSize:(NSString *)txt Font:(UIFont *)font Size:(CGSize)size { NSMutableA ...

  9. iOS中动态计算不同颜色、字体的文字高度

    在改项目bug的时候,有一个问题动态计算label的高度,前开发者竟然用字符串长度除以14.16这样的常量来计算是否换行,结果cell的高度问题非常严重. 因为label内容里有部分关键字是要另一种颜 ...

随机推荐

  1. 关于网站子目录绑定二级域名的方法(php网站手机端)

    最近帮客户做zencart网站手机模板用到了二级域名,通过判断手机访问来调用二级目录程序,http://afish.cnblogs.com/ 怎么说都比 http://www.cnblogs.com/ ...

  2. 详解mpstat等性能监测命令的使用

    mpstat是Multiprocessor Statistics的缩写,是实时监控工具,报告与cpu的一些统计信息这些信息都存在/proc/stat文件中,在多CPU系统里,其不但能查看所有的CPU的 ...

  3. 学习elasticsearch(一)linux环境搭建(1)

    首先安装了Oracle Virtual Box 然后安装了最小版的CentOS.由于vbox自带的操作面板不太好用,于是用了xshell,XShell连接最小版的centOS时遇到的问题记录下. 1. ...

  4. R的数据结构--向量

    向量:用于存储数值型.字符型或逻辑型数据的一维数组,只可以包含一种数据 向量的创建与运算 创建向量 # 创建简单向量 l <- c(2, 2, 1, 3, 8) # [1] 2 2 1 3 8 ...

  5. opengles reference card

    https://www.khronos.org/files/opengles31-quick-reference-card.pdf https://www.khronos.org/opengles/s ...

  6. SpringBoot统一异常处理后TX-LCN分布式事务无法捕获异常进行回滚

    通常我们使用SpringBoot都会进行统一异常处理,例如写一个BaseController,在BaseController里进行统一异常处理,然后其他的Controller都继承BaseContro ...

  7. BZOJ 3744 Gty的妹子序列 做法集结

    我只会O(nnlogn)O(n\sqrt nlogn)O(nn​logn)的 . . . . 这是分块+树状数组+主席树的做法O(nnlogn)O(n\sqrt nlogn)O(nn​logn) 搬来 ...

  8. Luogu P2516 [HAOI2010]最长公共子序列 DP

    首先$LIS$显然:$f[i][j]=max(f[i][j-1],f[i-1][j],(a[i]==b[j])*f[i-1][j-1])$ 考虑如何转移数量: 首先,不管$a[i]$是否等于$b[j] ...

  9. PC打开AS400 folder

    1.首先确保在iSeriers能打开该folder  Wrklink 'QDLS'  如果出现如下错误,则需要增加user profile到Directory Entries list CPF9006 ...

  10. 求二叉树的层次遍历(SDUT 2824)

    Problem Description 已知一颗二叉树的前序遍历和中序遍历,求二叉树的层次遍历. Input 输入数据有多组,输入T,代表有T组测试数据.每组数据有两个长度小于50的字符串,第一个字符 ...