实现效果如下:

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

实现思路:

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:

  1. #import <UIKit/UIKit.h>
  2. typedef void (^ReloadBlock)();
  3. @interface WebviewCell : UITableViewCell
  4.  
  5. @property(nonatomic, copy) NSString *htmlString;
  6. @property(nonatomic, copy) ReloadBlock reloadBlock;
  7.  
  8. +(CGFloat)cellHeight;
  9.  
  10. @end
  1. #import "WebviewCell.h"
  2. @interface WebviewCell()<UIWebViewDelegate>
  3.  
  4. @property(nonatomic,strong)UIWebView *webview;
  5.  
  6. @end
  7. static CGFloat staticheight = ;
  8.  
  9. @implementation WebviewCell
  10.  
  11. +(CGFloat)cellHeight
  12. {
  13. return staticheight;
  14. }
  15. -(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  16. {
  17. if (self = [super initWithStyle: style reuseIdentifier:reuseIdentifier]) {
  18.  
  19. // self.webview.scrollView.backgroundColor =[UIColor redColor];
  20. [self.contentView addSubview:self.webview];
  21. }
  22. return self;
  23.  
  24. }
  25. -(void)setHtmlString:(NSString *)htmlString
  26. {
  27. _htmlString = htmlString;
  28.  
  29. self.webview.delegate = self;
  30. // 否则导致网页下方留出了多余的空白
  31. // 手动改变图片适配问题,拼接html代码后,再加载html代码
  32. NSString *myStr = [NSString stringWithFormat:@"<head><style>img{max- width:%f !important;}</style></head>", [UIScreen mainScreen].bounds.size.width - ];
  33. NSString *str = [NSString stringWithFormat:@"%@%@",myStr, htmlString];
  34. [self.webview loadHTMLString:str baseURL:nil];
  35. }
  36.  
  37. -(void)webViewDidFinishLoad:(UIWebView *)webView
  38. {
  39. CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue]+ ;
  40. self.webview.frame = CGRectMake(, , K_SCREEN_WIDTH, height);
  41. self.webview.hidden = NO;
  42. if (staticheight != height+) {
  43. staticheight = height+;
  44. if (staticheight > ) {
  45. if (_reloadBlock) {
  46. _reloadBlock();
  47. }
  48. }
  49. }
  50. }
  51.  
  52. -(UIWebView *)webview {
  53.  
  54. if (!_webview) {
  55. _webview =[[UIWebView alloc]initWithFrame:CGRectMake(, , K_SCREEN_WIDTH, )];
  56. _webview.userInteractionEnabled = NO;
  57. _webview.hidden = YES;
  58. }
  59. return _webview;
  60. }
  61. @end

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

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

  1. #import "NewDetailViewController.h"
  2. #import "NewDetailHeaderView.h"
  3. #import "CommentBottomView.h"
  4. #import "HTTPTool.h"
  5. #import "HDNewsDetailModel.h"
  6. #import "News.h"
  7. #import "UIColor+HexColor.h"
  8. #import "NewDetailCommentModel.h"
  9. #import "WebviewCell.h"
  10. #import "DetailCommentCell.h"
  11.  
  12. @interface NewDetailViewController ()<UITableViewDelegate, UITableViewDataSource, UIWebViewDelegate>
  13.  
  14. @property (nonatomic, strong) UIWebView *webView;
  15. @property (nonatomic, strong) NewDetailHeaderView *headerView;
  16. @property (nonatomic, strong) CommentBottomView *bottomView;
  17. @property (nonatomic, assign) CGFloat keyBoardHeight;
  18. @property (nonatomic, assign) CGFloat currentTextViewHeight;
  19. @property (nonatomic,strong) NSMutableArray *array;
  20. @property (nonatomic, strong) NSArray *commentArray; // 评论model数组
  21. @property (nonatomic, strong) UITableView *commnetTableView;
  22. @property (nonatomic, strong) HDNewsDetailModel *detailModel;
  23.  
  24. @end
  25.  
  26. @implementation NewDetailViewController
  27.  
  28. - (void)viewDidLoad {
  29. [super viewDidLoad];
  30. self.navigationItem.title = @"新闻详情页";
  31. self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor], NSFontAttributeName : [UIFont boldSystemFontOfSize:]};
  32. self.navigationController.navigationBar.barTintColor = [UIColor colorWithHex:@"#4bb6ac"];
  33. self.automaticallyAdjustsScrollViewInsets = NO;
  34. self.navigationController.navigationBar.translucent = NO;
  35. self.currentTextViewHeight = ;
  36. [self addNotification];
  37. [self setupHeaderView];
  38. [self setupBottomView];
  39. [self setupUI];
  40. [self addNavShareItem];
  41. [self requestData];
  42. [self requestCommentData];
  43. }
  44.  
  45. - (void)addNavShareItem {
  46. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"1x_fenxiang"] style:UIBarButtonItemStylePlain target:self action:@selector(shareAction:)];
  47. self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
  48. }
  49.  
  50. #pragma mark - 键盘通知
  51. - (void)addNotification {
  52.  
  53. // 监听键盘的弹出
  54. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
  55. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
  56. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeHeight:) name:@"changeHeight" object:nil];
  57.  
  58. }
  59.  
  60. - (void)keyboardWillShow:(NSNotification *) notification {
  61.  
  62. float animationDuration = [[[notification userInfo] valueForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
  63. CGFloat height = [[[notification userInfo]objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size.height;
  64. self.keyBoardHeight = height;
  65. // 没有弹出键盘 使用这种动画比较顺畅一点
  66. [UIView animateWithDuration:animationDuration animations:^{
  67. CGRect bottomBarFrame = self.bottomView.frame;
  68. bottomBarFrame.origin.y = [UIScreen mainScreen].bounds.size.height - height - self.currentTextViewHeight - ;
  69. // CGRect rc = [self.view convertRect: self.bottomView.frame toView:self.view];
  70. [self.bottomView setFrame:bottomBarFrame];
  71. // 这里注意: 需要把底部view移动到最前面显示
  72. [self.view bringSubviewToFront:self.bottomView];
  73. }];
  74. }
  75.  
  76. - (void)keyboardWillHide:(NSNotification *) notification {
  77. float animationDuration = [[[notification userInfo] valueForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
  78. CGFloat height = [[[notification userInfo]objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
  79. self.keyBoardHeight = height;
  80. [UIView animateWithDuration:animationDuration animations:^{
  81. CGRect bottomBarFrame = self.bottomView.frame;
  82. bottomBarFrame.origin.y = self.view.bounds.size.height - self.currentTextViewHeight;
  83. [self.bottomView setFrame:bottomBarFrame];
  84. }];
  85. }
  86.  
  87. - (void)changeHeight: (NSNotification *)notifi {
  88. CGFloat height = [notifi.userInfo[@"height"] floatValue];
  89. if (height > ) {
  90.  
  91. self.currentTextViewHeight = height;
  92. }
  93. NSLog(@"height --- %f",height);
  94. [UIView animateWithDuration:0.2 animations:^{
  95. CGRect bottomBarFrame = self.bottomView.frame;
  96. bottomBarFrame.origin.y = self.view.bounds.size.height - height - self.keyBoardHeight;
  97. bottomBarFrame.size.height = height + ;
  98. [self.bottomView setFrame:bottomBarFrame];
  99. }];
  100. }
  101.  
  102. #pragma mark - 设置界面
  103.  
  104. - (void)setupUI {
  105. [self.view addSubview:self.commnetTableView];
  106. self.commnetTableView.frame = CGRectMake(, , K_SCREEN_WIDTH, K_SCREEN_HEIGHT - - );
  107. }
  108.  
  109. - (void)setupHeaderView {
  110.  
  111. self.headerView = [[NewDetailHeaderView alloc]initWithFrame:CGRectMake(, , [UIScreen mainScreen].bounds.size.width, )];
  112. self.headerView.focusButtonBlock = ^(BOOL flag) {
  113. NSLog(@"flag -- %d",flag);
  114. };
  115. [self.commnetTableView setTableHeaderView:self.headerView];
  116. }
  117.  
  118. - (void)setupBottomView {
  119. self.bottomView = [[CommentBottomView alloc]initWithFrame:CGRectMake(, [UIScreen mainScreen].bounds.size.height - - , [UIScreen mainScreen].bounds.size.width, )];
  120. [self.view addSubview:self.bottomView];
  121. }
  122.  
  123. #pragma mark - 数据请求
  124.  
  125. - (void)requestData {
  126. NSString *url = [NSString stringWithFormat:@"http://huidu.zhonghuilv.net/Index/newslist?newsid=%@",@(self.model.id)];
  127. [HTTPTool postWithURL:url headers:nil params:nil success:^(id json) {
  128. NSLog(@"json = %@",json);
  129. self.detailModel = [[HDNewsDetailModel alloc]init];
  130. [self.detailModel setValuesForKeysWithDictionary:json];
  131. // self.headerView.model = self.detailModel;
  132. [self.headerView configureHeaderAvaterWithImage:json[@"thumb"] title:json[@"title"]];
  133. } failure:^(NSError *error) {
  134. [CombancHUD showInfoWithStatus:@"加载失败"];
  135. }];
  136. }
  137.  
  138. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  139.  
  140. return self.commentArray.count + ;
  141. }
  142.  
  143. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  144.  
  145. if (indexPath.row == ) {
  146. WebviewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WebviewCell" forIndexPath:indexPath];
  147. cell.htmlString = self.detailModel.content;
  148. __weak NewDetailViewController *weakSelf = self;
  149. cell.reloadBlock =^()
  150. { // 刷新webViewCell
  151. [weakSelf.commnetTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
  152. };
  153. return cell;
  154. }
  155. DetailCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CommentCell" forIndexPath:indexPath];
  156. cell.model = self.commentArray[indexPath.row - ];
  157. return cell;
  158. }
  159.  
  160. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  161.  
  162. if (indexPath.row == ) {
  163. NSLog(@"cellHeight---%f",[WebviewCell cellHeight]);
  164. return [WebviewCell cellHeight];
  165. } else {
  166. return ;
  167. }
  168. }
  169.  
  170. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  171.  
  172. return 0.001f;
  173. }
  174.  
  175. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  176.  
  177. return 0.001f;
  178. }
  179.  
  180. - (void)requestCommentData {
  181. // index/Articlepl
  182. NSString *url = [NSString stringWithFormat:@"http://huidu.zhonghuilv.net/index/Articlepl?newsid=%@",@(self.model.id)];
  183. [HTTPTool postWithURL:url headers:nil params:nil success:^(id json) {
  184. NSLog(@"json = %@",json);
  185. self.commentArray = [NewDetailCommentModel mj_objectArrayWithKeyValuesArray:json];
  186. [self.commnetTableView reloadData];
  187. } failure:^(NSError *error) {
  188. [CombancHUD showInfoWithStatus:@"加载失败"];
  189. }];
  190. }
  191.  
  192. #pragma mark - Private Method
  193.  
  194. - (void)shareAction: (UIBarButtonItem *)item {
  195. NSLog(@"分享");
  196. }
  197.  
  198. - (UITableView *)commnetTableView {
  199. if (!_commnetTableView) {
  200. _commnetTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
  201. _commnetTableView.delegate = self;
  202. _commnetTableView.dataSource = self;
  203. [_commnetTableView registerNib:[UINib nibWithNibName:@"DetailCommentCell" bundle:nil] forCellReuseIdentifier:@"CommentCell"];
  204. [_commnetTableView registerClass:[WebviewCell class] forCellReuseIdentifier:@"WebviewCell"];
  205. _commnetTableView.tableHeaderView = [UIView new];
  206. _commnetTableView.tableFooterView = [UIView new];
  207. }
  208. return _commnetTableView;
  209. }
  210.  
  211. @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. 【Java 基础项目 - - Bank项目4】 对象构造/跨package调用

    UML设计: 文件组织: (注: 在bank4中,直接调用bank3的内容, 不再重复编写代码即可!) 代码编写Bank.java: package Banking_4; import Banking ...

  2. CSCD核心,北大中文核心

    从两篇文章看两个杂志 title 子空间聚类的重建模型及其快速算法 稀疏正则非凸优化问题之全局收敛分析 author 夏雨晴(浙江大学数学科学学院),张振跃 储敏(武汉大学数学与统计学院) journ ...

  3. WinForm DevExpress使用之ChartControl控件绘制图表二——进阶

    1. 多坐标折线图 在这个项目中,我需要做不同采集地方和不同数据类型的数据对比,自然而然就用到了多重坐标轴,多重坐标轴可以是多个X轴,也可以是Y轴,它们的处理方式类似.本文通过项目中的实际例子介绍多重 ...

  4. Bootstrap-轮播图-No.2

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...

  5. HDU 6088 - Rikka with Rock-paper-scissors | 2017 Multi-University Training Contest 5

    思路和任意模数FFT模板都来自 这里 看了一晚上那篇<再探快速傅里叶变换>还是懵得不行,可能水平还没到- - 只能先存个模板了,这题单模数NTT跑了5.9s,没敢写三模数NTT,可能姿势太 ...

  6. Python模块之目录

     1.加密算法有关 hmac模块 hashlib模块 2.进程有关 multiprocessing模块 3.线程有关 threading模块 4.协程有关 asyncio模块 5.系统命令调用 sub ...

  7. SpringBoot 测试类 @RunWith & @SpringBootTest

    @RunWith(SpringRunner.class) @SpringBootTest public class RabbitMqTest { @Autowired RabbitMqSender r ...

  8. Java 面试题 三 <JavaWeb应用调优线程池 JVM原理及调优>

    1.Java Web应用调优线程池 不论你是否关注,Java Web应用都或多或少的使用了线程池来处理请求.线程池的实现细节可能会被忽视,但是有关于线程池的使用和调优迟早是需要了解的.本文由浅入深,介 ...

  9. 用javascript来判别回文数

    什么是回文数?通俗的说就是正着读和倒着读都一样的字符串(即使是数字也是可以看成字符串的). 所以下面回文数都是用字符串来表示的,即判断回文数就是对字符串的判断. 举几个回文数的例子: i love u ...

  10. python 装饰器,传递类以及参数

    #!/usr/bin/env python # coding=utf- import time #import redis class RedisLock(object): def __init__( ...