iOS实现页面既显示WebView,WebView下显示TableView,动态计算WebView内容高度
实现效果如下:
忽略底部的评论视图,太丑了,待完善......
实现思路:
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内容高度的更多相关文章
- iOS View自定义窍门——UIButton实现上显示图片,下显示文字
“UIButton实现上显示图片,下显示文字”这个需求相信大家在开发中都或多或少会遇见.比如自定义分享View的时候.当然,也可以封装一个item,上边imageView,下边一个label.但是既然 ...
- iOS 动态计算文本内容的高度
关于ios 下动态计算文本内容的高度,经过查阅和网上搜素,现在看到的有以下几种方法: 1. // 获取字符串的大小 ios6 - (CGSize)getStringRect_:(NSString* ...
- nodejs之获取客户端真实的ip地址+动态页面中引用静态路径下的文件及图片等内容
1.nodejs获取客户端真实的IP地址: 在一般的管理网站中,尝尝会需要将用户的一些操作记录下来,并记住是哪个用户进行操作的,这时需要用户的ip地址,但是往往当这些应用部署在服务器上后,都使用了ng ...
- iOS开发总结-UITableView 自定义cell和动态计算cell的高度
UITableView cell自定义头文件:shopCell.h#import <UIKit/UIKit.h>@interface shopCell : UITableViewCell@ ...
- iOS学习之根据文本内容动态计算文本框高度的步骤
在视图加载的过程中,是先计算出frame,再根据frame加载视图的,所以在设计计算高度的方法的时候,设计成加号方法; //首先给外界提供计算cell高度的方法 + (CGFloat)heightFo ...
- iOS问题处理:如何在Mac下显示Finder中的所有文件
摘自:http://www.cnblogs.com/elfsundae/archive/2010/11/30/1892544.html 在Unix下工作,你可能需要处理一些“特殊“文件或文件夹,例如/ ...
- Ionic+AngularJS 开发的页面在微信公众号下显示不出来原因查究
ionic 页面 微信浏览器遇到的坑 公司的微信公众号一部分页面是用AngularJS+Ioinc开发,发现在本地浏览器测试的时候都没问题,传到服务器在微信公众号下跑就出问题来,经查是: index- ...
- iOS之动态计算文字的高度
+ (CGSize)boundingALLRectWithSize:(NSString *)txt Font:(UIFont *)font Size:(CGSize)size { NSMutableA ...
- iOS中动态计算不同颜色、字体的文字高度
在改项目bug的时候,有一个问题动态计算label的高度,前开发者竟然用字符串长度除以14.16这样的常量来计算是否换行,结果cell的高度问题非常严重. 因为label内容里有部分关键字是要另一种颜 ...
随机推荐
- 【Java 基础项目 - - Bank项目4】 对象构造/跨package调用
UML设计: 文件组织: (注: 在bank4中,直接调用bank3的内容, 不再重复编写代码即可!) 代码编写Bank.java: package Banking_4; import Banking ...
- CSCD核心,北大中文核心
从两篇文章看两个杂志 title 子空间聚类的重建模型及其快速算法 稀疏正则非凸优化问题之全局收敛分析 author 夏雨晴(浙江大学数学科学学院),张振跃 储敏(武汉大学数学与统计学院) journ ...
- WinForm DevExpress使用之ChartControl控件绘制图表二——进阶
1. 多坐标折线图 在这个项目中,我需要做不同采集地方和不同数据类型的数据对比,自然而然就用到了多重坐标轴,多重坐标轴可以是多个X轴,也可以是Y轴,它们的处理方式类似.本文通过项目中的实际例子介绍多重 ...
- Bootstrap-轮播图-No.2
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...
- HDU 6088 - Rikka with Rock-paper-scissors | 2017 Multi-University Training Contest 5
思路和任意模数FFT模板都来自 这里 看了一晚上那篇<再探快速傅里叶变换>还是懵得不行,可能水平还没到- - 只能先存个模板了,这题单模数NTT跑了5.9s,没敢写三模数NTT,可能姿势太 ...
- Python模块之目录
1.加密算法有关 hmac模块 hashlib模块 2.进程有关 multiprocessing模块 3.线程有关 threading模块 4.协程有关 asyncio模块 5.系统命令调用 sub ...
- SpringBoot 测试类 @RunWith & @SpringBootTest
@RunWith(SpringRunner.class) @SpringBootTest public class RabbitMqTest { @Autowired RabbitMqSender r ...
- Java 面试题 三 <JavaWeb应用调优线程池 JVM原理及调优>
1.Java Web应用调优线程池 不论你是否关注,Java Web应用都或多或少的使用了线程池来处理请求.线程池的实现细节可能会被忽视,但是有关于线程池的使用和调优迟早是需要了解的.本文由浅入深,介 ...
- 用javascript来判别回文数
什么是回文数?通俗的说就是正着读和倒着读都一样的字符串(即使是数字也是可以看成字符串的). 所以下面回文数都是用字符串来表示的,即判断回文数就是对字符串的判断. 举几个回文数的例子: i love u ...
- python 装饰器,传递类以及参数
#!/usr/bin/env python # coding=utf- import time #import redis class RedisLock(object): def __init__( ...