实现效果如下:

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

实现思路:

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

最新文章

  1. shell使用攻略
  2. EF中执行sql语句,以及事务
  3. .net开发笔记(十六) 对前部分文章的一些补充和总结
  4. struts2 提供的校验器列表
  5. dns服务
  6. linux下可以禁用的一些服务
  7. lucene 检索流程整理笔记
  8. poj 2711 Leapin&#39; Lizards &amp;&amp; BZOJ 1066: [SCOI2007]蜥蜴 最大流
  9. [GeekBand] STL与泛型编程(1)
  10. 【Ruby on Rails学习二】在线学习资料的整理
  11. K:线性表
  12. 最优的路线(floyd最小环)
  13. 时序数据库InfluxDB:简介及安装
  14. 第七章&#183; Redis Cluster 核心技术
  15. ActiveMQ-为什么需要消息中间件?
  16. 安装httpd服务
  17. python积累二:中文乱码解决方法
  18. python中Flask模块的使用
  19. linux 内核升级 转
  20. 20145127《java程序设计》第五周学习总结

热门文章

  1. 【Codeforces】Orz Panda Cup
  2. ubuntu18系统 Qt Error BadAccess
  3. Ubuntu安装截图软件shutter
  4. 【组合数学】AGC036C - GP 2
  5. STMStudio-stm32软件的应用笔记
  6. Mac OS 系统开发环境的一些坑
  7. 【Android】查看内存
  8. Win2008 R2 IIS FTP防火墙的配置
  9. Poj 3764 The xor-longest Path(Trie树+xor+贪心)
  10. ****题(alb)