Skip to content

Commit

Permalink
初步解决了评论的页面和cell动态高度
Browse files Browse the repository at this point in the history
  • Loading branch information
devlb committed Oct 19, 2015
1 parent f74a126 commit 57697e1
Show file tree
Hide file tree
Showing 20 changed files with 440 additions and 8 deletions.
Binary file modified .DS_Store
Binary file not shown.
24 changes: 24 additions & 0 deletions LBzhihu.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
2B6ECB581B982EFE00E3DD29 /* ContentList.m in Sources */ = {isa = PBXBuildFile; fileRef = 2B6ECB531B982EFE00E3DD29 /* ContentList.m */; };
2B6ECB591B982EFE00E3DD29 /* DetailsModel.m in Sources */ = {isa = PBXBuildFile; fileRef = 2B6ECB551B982EFE00E3DD29 /* DetailsModel.m */; };
2B6ECB5A1B982EFE00E3DD29 /* StoriesItm.m in Sources */ = {isa = PBXBuildFile; fileRef = 2B6ECB571B982EFE00E3DD29 /* StoriesItm.m */; };
2BA6117B1BD3E41F0038AD23 /* CommentsCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 2BA6117A1BD3E41F0038AD23 /* CommentsCell.m */; settings = {ASSET_TAGS = (); }; };
2BBF22B51BD3782A00DD391C /* CommentsController.m in Sources */ = {isa = PBXBuildFile; fileRef = 2BBF22B41BD3782A00DD391C /* CommentsController.m */; settings = {ASSET_TAGS = (); }; };
2BBF22C01BD37F3000DD391C /* CommentsItm.m in Sources */ = {isa = PBXBuildFile; fileRef = 2BBF22BD1BD37F3000DD391C /* CommentsItm.m */; settings = {ASSET_TAGS = (); }; };
2BBF22C11BD37F3000DD391C /* CommentsModel.m in Sources */ = {isa = PBXBuildFile; fileRef = 2BBF22BF1BD37F3000DD391C /* CommentsModel.m */; settings = {ASSET_TAGS = (); }; };
2BDAB2881B972088000ADE68 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 2BDAB2871B972088000ADE68 /* main.m */; };
2BDAB28B1B972088000ADE68 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 2BDAB28A1B972088000ADE68 /* AppDelegate.m */; };
2BDAB2911B972088000ADE68 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 2BDAB28F1B972088000ADE68 /* Main.storyboard */; };
Expand Down Expand Up @@ -88,6 +92,14 @@
2B6ECB551B982EFE00E3DD29 /* DetailsModel.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = DetailsModel.m; path = "Model/内容/DetailsModel.m"; sourceTree = "<group>"; };
2B6ECB561B982EFE00E3DD29 /* StoriesItm.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = StoriesItm.h; path = "Model/内容/StoriesItm.h"; sourceTree = "<group>"; };
2B6ECB571B982EFE00E3DD29 /* StoriesItm.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = StoriesItm.m; path = "Model/内容/StoriesItm.m"; sourceTree = "<group>"; };
2BA611791BD3E41F0038AD23 /* CommentsCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CommentsCell.h; path = Views/CommentsCell.h; sourceTree = "<group>"; };
2BA6117A1BD3E41F0038AD23 /* CommentsCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CommentsCell.m; path = Views/CommentsCell.m; sourceTree = "<group>"; };
2BBF22B31BD3782A00DD391C /* CommentsController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CommentsController.h; sourceTree = "<group>"; };
2BBF22B41BD3782A00DD391C /* CommentsController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CommentsController.m; sourceTree = "<group>"; };
2BBF22BC1BD37F3000DD391C /* CommentsItm.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CommentsItm.h; path = "Model/内容/CommentsItm.h"; sourceTree = "<group>"; };
2BBF22BD1BD37F3000DD391C /* CommentsItm.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CommentsItm.m; path = "Model/内容/CommentsItm.m"; sourceTree = "<group>"; };
2BBF22BE1BD37F3000DD391C /* CommentsModel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CommentsModel.h; path = "Model/内容/CommentsModel.h"; sourceTree = "<group>"; };
2BBF22BF1BD37F3000DD391C /* CommentsModel.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CommentsModel.m; path = "Model/内容/CommentsModel.m"; sourceTree = "<group>"; };
2BCB672A1B9ACA840046E417 /* libpop.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libpop.a; path = "Pods/../build/Debug-iphoneos/libpop.a"; sourceTree = "<group>"; };
2BCB672C1B9ACBA40046E417 /* libpop.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libpop.a; path = "Pods/../build/Debug-iphoneos/libpop.a"; sourceTree = "<group>"; };
2BDAB2821B972088000ADE68 /* LBzhihu.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = LBzhihu.app; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -152,6 +164,8 @@
2B6ECB351B982EC300E3DD29 /* DetailsController.m */,
2B6ECB361B982EC300E3DD29 /* HomeViewController.h */,
2B6ECB371B982EC300E3DD29 /* HomeViewController.m */,
2BBF22B31BD3782A00DD391C /* CommentsController.h */,
2BBF22B41BD3782A00DD391C /* CommentsController.m */,
);
name = Controller;
sourceTree = "<group>";
Expand Down Expand Up @@ -191,6 +205,8 @@
2B6ECB411B982ED200E3DD29 /* StoriesCell.m */,
2B6ECB421B982ED200E3DD29 /* TopView.h */,
2B6ECB431B982ED200E3DD29 /* TopView.m */,
2BA611791BD3E41F0038AD23 /* CommentsCell.h */,
2BA6117A1BD3E41F0038AD23 /* CommentsCell.m */,
);
name = Views;
sourceTree = "<group>";
Expand Down Expand Up @@ -219,6 +235,10 @@
2B6ECB571B982EFE00E3DD29 /* StoriesItm.m */,
2B6606601B9A99F300D87BD9 /* DetailsInfo.h */,
2B6606611B9A99F300D87BD9 /* DetailsInfo.m */,
2BBF22BC1BD37F3000DD391C /* CommentsItm.h */,
2BBF22BD1BD37F3000DD391C /* CommentsItm.m */,
2BBF22BE1BD37F3000DD391C /* CommentsModel.h */,
2BBF22BF1BD37F3000DD391C /* CommentsModel.m */,
);
name = "内容";
sourceTree = "<group>";
Expand Down Expand Up @@ -446,10 +466,14 @@
2B6ECB581B982EFE00E3DD29 /* ContentList.m in Sources */,
2BDAB2881B972088000ADE68 /* main.m in Sources */,
2B6ECB441B982ED200E3DD29 /* HomeHeadView.m in Sources */,
2BBF22C01BD37F3000DD391C /* CommentsItm.m in Sources */,
2B6ECB5A1B982EFE00E3DD29 /* StoriesItm.m in Sources */,
2B6ECB511B982EF300E3DD29 /* ThemeType.m in Sources */,
2B6ECB4B1B982EE400E3DD29 /* JsonModel.m in Sources */,
2B6ECB391B982EC300E3DD29 /* HomeViewController.m in Sources */,
2BBF22C11BD37F3000DD391C /* CommentsModel.m in Sources */,
2BBF22B51BD3782A00DD391C /* CommentsController.m in Sources */,
2BA6117B1BD3E41F0038AD23 /* CommentsCell.m in Sources */,
2B6ECB481B982ED200E3DD29 /* TopView.m in Sources */,
2B6ECB501B982EF300E3DD29 /* ThemeItm.m in Sources */,
);
Expand Down
Binary file not shown.
Binary file modified LBzhihu/.DS_Store
Binary file not shown.
16 changes: 16 additions & 0 deletions LBzhihu/CommentsController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// CommentsController.h
// LBzhihu
//
// Created by lb on 15/10/18.
// Copyright © 2015年 李波. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "CommentsController.h"

@interface CommentsController : UIViewController

@property (nonatomic,strong) NSString *storiesId;

@end
151 changes: 151 additions & 0 deletions LBzhihu/CommentsController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
//
// CommentsController.m
// LBzhihu
//
// Created by lb on 15/10/18.
// Copyright © 2015年 李波. All rights reserved.
//

#import "CommentsController.h"
#import "NetworkTool.h"
#import "UIImageView+WebCache.h"
#import "CommentsCell.h"

@interface CommentsController ()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>
{
NSMutableArray *data;
NSMutableArray *heightArr;
}

@property (nonatomic,strong) UITableView *tableView;

@end

@implementation CommentsController

- (void)viewDidLoad {
[super viewDidLoad];
data = [NSMutableArray arrayWithObjects:[NSMutableArray array],[NSMutableArray array],nil];
heightArr = [NSMutableArray arrayWithObjects:[NSMutableArray array],[NSMutableArray array],nil];

[self addSubView];
[self loadData];
}

- (void)addSubView{
self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:(UITableViewStyleGrouped)];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.allowsSelection = NO;
[self.view addSubview:self.tableView];

UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
swipe.delegate = self;
[self.view addGestureRecognizer:swipe];
}

- (void)loadData{
data = [NSMutableArray arrayWithObjects:[NSMutableArray array],[NSMutableArray array],nil];

[[NetworkTool sharedNetworkTool] getLongComments:self.storiesId success:^(CommentsModel *commentsModel) {
[data replaceObjectAtIndex:0 withObject:commentsModel.comments];

dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});

} failure:^{
[[[UIAlertView alloc] initWithTitle:@"获取评论失败" message:@"请稍后再试" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil] show];
}];

// [[NetworkTool sharedNetworkTool] getShortComments:self.storiesId success:^(CommentsModel *commentsModel) {
// [data replaceObjectAtIndex:1 withObject:commentsModel.comments];
//
// dispatch_async(dispatch_get_main_queue(), ^{
// [self.tableView reloadData];
// });
//
// } failure:^{
// [[[UIAlertView alloc] initWithTitle:@"获取评论失败" message:@"请稍后再试" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil] show];
// }];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [data[section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
CommentsItm *itm = data[indexPath.section][indexPath.row];

CommentsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil) {
cell = [[CommentsCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:@"cell"];
}
cell.userLbael.text = itm.author;
cell.likeNumberLabel.text = [NSString stringWithFormat:@"%@",itm.likes];
[cell.likeBtn setImage:[UIImage imageNamed:@"icon.bundle/Comment_Vote.png"] forState:(UIControlStateNormal)];
[cell.likeBtn addTarget:self action:@selector(like:) forControlEvents:(UIControlEventTouchUpInside)];
[cell.imgView sd_setImageWithURL:[NSURL URLWithString:itm.avatar] placeholderImage:[UIImage imageNamed:@"icon.bundle/[email protected]"]];
[cell setTextLabelText:itm.content];
cell.timeLabel.text = [self sectionStrByCreateTime:[itm.time floatValue]];

[heightArr[indexPath.section] addObject:@(cell.frame.size.height)];

return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if ([heightArr[0] count] > 0 || [heightArr[1] count] > 0) {
NSMutableArray *arr = heightArr[indexPath.section];
NSNumber *num = arr[indexPath.row];
return [num floatValue];

}
return 0.0;
}

//时间戳转为字符串
- (NSString *)sectionStrByCreateTime:(NSTimeInterval)interval{
static NSDateFormatter *formatter = nil;
if (nil == formatter) {
formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"MM-dd HH:mm";
}
NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];
return [formatter stringFromDate:date];
}

- (void)like:(UIButton *)btn{
NSArray *imgs = @[[UIImage imageNamed:@"icon.bundle/Comment_Vote.png"],[UIImage imageNamed:@"icon.bundle/[email protected]"]];
UIImage *image = [btn.imageView.image isEqual:imgs[0]] ? imgs[1] : imgs[0];
[btn setImage:image forState:(UIControlStateNormal)];
}

- (void)swipe:(UISwipeGestureRecognizer *)recognizer{
if (recognizer.direction == UISwipeGestureRecognizerDirectionRight) {
[self.navigationController popViewControllerAnimated:YES];
}
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/

@end
24 changes: 18 additions & 6 deletions LBzhihu/Controller/DetailsController.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
//#import "PubilcMember.m"
#import <MBProgressHUD.h>
#import "UIButton+Badge.h"
#import "DetailsController.m"
#import "CommentsController.h"

@interface DetailsController ()<UIWebViewDelegate,UIGestureRecognizerDelegate>
{
NSMutableArray *btns;
}
@property (nonatomic,strong) UIWebView *webView;
@property (nonatomic,strong) UIView *toolView;
@property (nonatomic,strong) MBProgressHUD *hud;

@end

Expand All @@ -33,6 +36,7 @@ - (void)viewDidLoad {
- (void)addUI{
self.view.backgroundColor = [UIColor whiteColor];
self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, MAINSIZE.width, MAINSIZE.height - DETAILTOOLVIEWH)];
self.webView.backgroundColor = [UIColor clearColor];
self.webView.scrollView.bounces = YES;
self.webView.delegate = self;

Expand Down Expand Up @@ -72,16 +76,16 @@ - (void)addUI{

- (void)loadData{

MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.labelText = @"正在加载网页";
self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
self.hud.labelText = @"正在加载网页";

StoriesItm *itm = self.storieArr[self.curIndex];

__weak typeof(self) weakSelf = self;
[[NetworkTool sharedNetworkTool] getDetailsWithStoriesId:itm.storiesId success:^(DetailsModel *detailsModel) {

dispatch_async(dispatch_get_main_queue(), ^{
[hud hide:YES];

[weakSelf showInWebViewWithDetailsModel:detailsModel];
});

Expand Down Expand Up @@ -126,12 +130,11 @@ - (void)showInWebViewWithDetailsModel:(DetailsModel *)model{
[htmlString appendFormat:@" rel=""stylesheet"" type=""text/css"">"];
}

[htmlString appendFormat:@"<style>.avatarT {width:100%%;height:200px; position:absolute; min-width:320px;} .bgwz {position:absolute; color:#fff; margin:150px 15px 0 15px; min-width:290px; font-size:1em;}</style>"];

[htmlString appendFormat:@"<style>.avatarT {width:100%;height:200px; position:absolute; min-width:320px;} .bgwz {position:absolute; color:#fff; margin:150px 15px 0 15px; min-width:290px; font-size:1em;}</style>"];


[htmlString appendFormat:@"<body>"];

if (model.image.length > 0) {
[htmlString appendFormat:@"<div><img class=""avatarT"" src=""%@""><p class=""bgwz"">%@</p></div>",model.image,model.title];
}
Expand All @@ -154,6 +157,10 @@ - (void)showInWebViewWithDetailsModel:(DetailsModel *)model{
}


- (void)webViewDidFinishLoad:(UIWebView *)webView{
[self.hud hide:YES];
}

- (void)swipe:(UISwipeGestureRecognizer *)recognizer{
if (recognizer.direction == UISwipeGestureRecognizerDirectionRight) {
[self back];
Expand Down Expand Up @@ -186,6 +193,11 @@ - (void)share{
- (void)comments{

NSLog(@"评论");
StoriesItm *itm = self.storieArr[self.curIndex];

CommentsController *commentVC = [CommentsController new];
commentVC.storiesId = itm.storiesId;
[self.navigationController pushViewController:commentVC animated:YES];
}

- (void)viewWillAppear:(BOOL)animated{
Expand Down
2 changes: 1 addition & 1 deletion LBzhihu/Controller/HomeViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ - (void)addUI{
[containerView addSubview:self.topView];
[containerView addSubview:pageControl];

self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0, MAINSIZE.width, MAINSIZE.height - CGRectGetMaxY(self.headView.frame)) style:(UITableViewStylePlain)];
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0, MAINSIZE.width, MAINSIZE.height) style:(UITableViewStylePlain)];
self.tableView.tableHeaderView = containerView;
self.tableView.delegate = self;
self.tableView.dataSource = self;
Expand Down
Binary file modified LBzhihu/Model/.DS_Store
Binary file not shown.
Binary file modified LBzhihu/Model/内容/.DS_Store
Binary file not shown.
19 changes: 19 additions & 0 deletions LBzhihu/Model/内容/CommentsItm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// CommentsModel.h
// LBzhihu
//
// Created by lb on 15/10/18.
// Copyright © 2015年 李波. All rights reserved.
//

#import "JsonModel.h"

@interface CommentsItm : JsonModel

@property (nonatomic,strong) NSString *author;
@property (nonatomic,strong) NSString *content;
@property (nonatomic,strong) NSString *avatar;
@property (nonatomic,strong) NSString *time;
@property (nonatomic,strong) NSString *likes;

@end
13 changes: 13 additions & 0 deletions LBzhihu/Model/内容/CommentsItm.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// CommentsModel.m
// LBzhihu
//
// Created by lb on 15/10/18.
// Copyright © 2015年 李波. All rights reserved.
//

#import "CommentsItm.h"

@implementation CommentsItm

@end
16 changes: 16 additions & 0 deletions LBzhihu/Model/内容/CommentsModel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// CommentsItmModel.h
// LBzhihu
//
// Created by lb on 15/10/18.
// Copyright © 2015年 李波. All rights reserved.
//

#import "JsonModel.h"
#import "CommentsItm.h"

@interface CommentsModel : JsonModel

@property (nonatomic,strong) NSArray *comments;

@end
Loading

0 comments on commit 57697e1

Please sign in to comment.