//
|
// allCommentsViewController.m
|
// BuWanVideo2.0
|
//
|
// Created by apple on 17/1/4.
|
// Copyright © 2017年 com.yeshi.buwansheque.ios. All rights reserved.
|
//
|
|
#import "allCommentsViewController.h"
|
#import "findcommentTableViewCell.h"
|
|
#import "LoggingViewController.h"
|
|
@interface allCommentsViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate,UIGestureRecognizerDelegate>{
|
UITableView *_tableView;
|
|
UIView *_replyMessageView;//回复的输入视图
|
UIButton *sendButton;//回复按钮
|
UITextField *_textField;//回复的输入框
|
int _commentPage;
|
|
NSMutableArray *commentArr;
|
|
UIButton *commitButton;
|
}
|
|
@end
|
|
@implementation allCommentsViewController
|
|
- (void)viewDidLoad {
|
[super viewDidLoad];
|
//增加通知中心监听,当键盘出现或消失时收出消息
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardWillShowNotification object:nil];
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
|
|
//默认请求第一页的评论信息
|
_commentPage=1;
|
//创建界面
|
[self creatUI];
|
}
|
|
-(void)creatUI{
|
//返回按钮
|
UIButton *backButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
|
backButton.frame=CGRectMake(0, 0, 35, 44);
|
[backButton setTitle:@"返回" forState:UIControlStateNormal];
|
[backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
|
backButton.titleLabel.font = [UIFont systemFontOfSize:17.0];
|
[backButton addTarget:self action:@selector(BackClick:) forControlEvents:UIControlEventTouchUpInside];
|
UIBarButtonItem *backBtnItem=[[UIBarButtonItem alloc] initWithCustomView:backButton];
|
self.navigationItem.leftBarButtonItem=backBtnItem;
|
|
//评论列表
|
_tableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, KScreenW, KScreenH) style:UITableViewStyleGrouped];
|
_tableView.delegate=self;
|
_tableView.dataSource=self;
|
_tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
|
_tableView.backgroundColor=kGlobalBackgroundColor;
|
_tableView.mj_header=[MJRefreshStateHeader headerWithRefreshingBlock:^{
|
_commentPage=1;
|
[self loadCommentData:nil];
|
}];
|
_tableView.mj_footer=[MJRefreshAutoFooter footerWithRefreshingBlock:^{
|
[self loadCommentData:nil];
|
}];
|
|
//注册cell
|
[_tableView registerNib:[UINib nibWithNibName:@"findcommentTableViewCell" bundle:nil] forCellReuseIdentifier:@"findcommentTableViewCell"];
|
[self.view addSubview:_tableView];
|
|
//加载评论数据
|
[self loadCommentData:@"疯狂加载中"];
|
|
//评论框
|
[self CommentBox];
|
}
|
|
-(void)loadCommentData:(NSString *)str{
|
if (str!=nil) {
|
[SVProgressHUD showWithStatus:str];
|
}
|
[[YTHNetInterface startInterface] getCommentListWithUid:[YTHsharedManger startManger].Uid WithSystem:@"1" withId:_goodsId withPage:[NSString stringWithFormat:@"%d",_commentPage] withBlock:^(BOOL isSuccessful, id result, NSString *error) {
|
if (isSuccessful) {
|
if (str!=nil) {
|
[SVProgressHUD dismiss];
|
}
|
if (!commentArr) {
|
commentArr=[NSMutableArray arrayWithCapacity:0];
|
}
|
if (_commentPage==1) {
|
[commentArr removeAllObjects];
|
}
|
NSArray *arr=[[result objectForKey:@"Data"] objectForKey:@"data"];
|
if ([arr count]>0) {
|
++_commentPage;
|
[commentArr addObjectsFromArray:arr];
|
[_tableView reloadData];
|
}else{
|
[SVProgressHUD dismiss];
|
}
|
}else{
|
[SVProgressHUD showErrorWithStatus:@"数据加载失败"];
|
NSLog(@"%@",error);
|
}
|
}];
|
[_tableView.mj_header endRefreshing];
|
[_tableView.mj_footer endRefreshing];
|
}
|
|
-(void)PostComment{
|
if([[NSUserDefaults standardUserDefaults] boolForKey:@"userOnLine"]){
|
[self HideBox];
|
//已登录
|
[[YTHNetInterface startInterface] addCommentWithUid:[YTHsharedManger startManger].Uid WithSystem:@"1" withId:_goodsId withLoginUid:[[NSUserDefaults standardUserDefaults] objectForKey:@"LoginUid"] withContent:_textField.text withBlock:^(BOOL isSuccessful, id result, NSString *error) {
|
if (isSuccessful) {
|
[SVProgressHUD showSuccessWithStatus:@"评论成功"];
|
[self HideBox];
|
_commentPage=1;
|
[self loadCommentData:@"刷新中..."];
|
_textField.text=@"";
|
}else{
|
[SVProgressHUD showErrorWithStatus:@"评论失败"];
|
}
|
}];
|
}else{
|
//未登录,引导用户登录
|
LoggingViewController *loginVC=[[LoggingViewController alloc] init];
|
loginVC.ispresent=YES;
|
[self presentViewController:loginVC animated:YES completion:^{
|
|
}];
|
}
|
}
|
|
-(void)CommentBox{
|
commitButton=[[UIButton alloc] initWithFrame:CGRectMake(KScreenW-20-40, KScreenH-30-40, 40, 40)];
|
[commitButton setImage:[UIImage imageNamed:@"全部评论界面按钮"] forState:UIControlStateNormal];
|
commitButton.layer.masksToBounds=YES;
|
commitButton.layer.cornerRadius=20.0;
|
[commitButton addTarget:self action:@selector(displayBox) forControlEvents:UIControlEventTouchUpInside];
|
[self.view addSubview:commitButton];
|
|
//创建手势事件
|
UITapGestureRecognizer *Tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(resignResponder)];
|
[_tableView addGestureRecognizer:Tap];
|
Tap.delegate=self;
|
|
UIImageView *backimage = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, KScreenW-80 , 30)];
|
backimage.image=[[UIImage imageNamed:@"输入框"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 40, 20, backimage.frame.size.width-40)];
|
|
//回复的输入视图
|
_replyMessageView=[[UIView alloc] initWithFrame:CGRectMake(0, _tableView.frame.size.height-50, KScreenW, 50)];
|
_replyMessageView.backgroundColor=[UIColor whiteColor];
|
|
//回复的输入框
|
_textField=[[UITextField alloc] initWithFrame:CGRectMake(20, 10, KScreenW-90, 30)];
|
_textField.delegate=self;
|
_textField.backgroundColor=kGlobalBackgroundColor;
|
_textField.clearButtonMode = UITextFieldViewModeWhileEditing;
|
_textField.placeholder=@" 我也来说两句...";
|
|
//回复发送按钮
|
sendButton=[[UIButton alloc] initWithFrame:CGRectMake(KScreenW-60, 10, 50, 30)];
|
[sendButton setBackgroundImage:[[UIImage imageNamed:@"发表"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]forState:UIControlStateNormal];
|
|
[sendButton setTitleColor:kGlobalMainColor forState:UIControlStateNormal];
|
[sendButton setTitle:@"发送" forState:UIControlStateNormal];
|
[sendButton addTarget:self action:@selector(PostComment) forControlEvents:UIControlEventTouchUpInside];
|
|
[_replyMessageView addSubview:backimage];
|
[_replyMessageView addSubview:_textField];
|
[_replyMessageView addSubview:sendButton];
|
|
[self.view addSubview:_replyMessageView];
|
[_replyMessageView setHidden:YES];
|
}
|
|
-(void)displayBox{
|
[_replyMessageView setHidden:NO];
|
[_textField becomeFirstResponder];
|
[commitButton setHidden:YES];
|
}
|
|
-(void)HideBox{
|
[_textField resignFirstResponder];
|
[_replyMessageView setHidden:YES];
|
[commitButton setHidden:NO];
|
}
|
|
-(void)BackClick:(UIButton *)sender{
|
[SVProgressHUD dismiss];
|
[self.navigationController popViewControllerAnimated:NO];
|
}
|
|
-(void)resignResponder{
|
[self HideBox];
|
}
|
|
#pragma mark -UITableViewDataSource
|
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
|
return [commentArr count];
|
}
|
|
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
|
findcommentTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"findcommentTableViewCell" forIndexPath:indexPath];
|
NSDictionary *dic=commentArr[indexPath.row];
|
|
NSString *tempIconStr=[[dic objectForKey:@"User"] objectForKey:@"Portrait"];//保存头像地址
|
|
//判断头像地址是否为完整的地址
|
if (tempIconStr.length>5) {
|
if (![[tempIconStr substringWithRange:NSMakeRange(0, 4)] isEqualToString:@"http"]) {//不是完整的http地址
|
tempIconStr=[NSString stringWithFormat:@"%@%@",iconImageUrl,[[dic objectForKey:@"User"] objectForKey:@"Portrait"]];
|
}
|
[cell.userIconImage setYthImageWithURL:tempIconStr placeholderImage:[UIImage imageNamed:@"关注默认头像"]];
|
}else{
|
[cell.userIconImage setImage:[UIImage imageNamed:@"关注默认头像"]];
|
}
|
|
cell.userName.text=[[dic objectForKey:@"User"] objectForKey:@"Nickname"];
|
cell.userTime.text=[[YTHNetInterface startInterface] getDate:[dic objectForKey:@"Createtime"]];
|
cell.userContent.text=[dic objectForKey:@"Content"];
|
return cell;
|
}
|
|
#pragma mark -UITableViewDelegate
|
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
|
NSDictionary *dic=commentArr[indexPath.row];
|
NSString *str=[dic objectForKey:@"Content"];
|
CGSize commentSize = [str boundingRectWithSize:CGSizeMake(KScreenW-44, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size;
|
return commentSize.height+65;
|
}
|
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
|
return 50;
|
}
|
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
|
return 0;
|
}
|
|
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
|
UIView *HeaderView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenW, 50)];
|
HeaderView.backgroundColor=kGlobalBackgroundColor;
|
|
UIView *contentView=[[UIView alloc] initWithFrame:CGRectMake(10, 13.5, KScreenW-20, 36)];
|
contentView.backgroundColor=[UIColor whiteColor];
|
[HeaderView addSubview:contentView];
|
|
UILabel *titleLabel=[[UILabel alloc] initWithFrame:CGRectMake(8, 0, 40, 36)];
|
titleLabel.font=[UIFont systemFontOfSize:15];
|
titleLabel.text=@"评论";
|
[contentView addSubview:titleLabel];
|
|
UILabel *numLabel=[[UILabel alloc] initWithFrame:CGRectMake(48, 0, 30, 36)];
|
numLabel.font=[UIFont systemFontOfSize:15];
|
numLabel.text=[NSString stringWithFormat:@"%d",(int)[commentArr count]];
|
numLabel.textColor=YTHColor(155, 125, 98);
|
[contentView addSubview:numLabel];
|
|
return HeaderView;
|
}
|
|
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath{
|
return NO;
|
}
|
|
#pragma mark -UITextFieldDelegate
|
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
|
[_tableView setScrollEnabled:NO];
|
return YES;
|
}
|
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
|
[self.view endEditing:YES];
|
[self PostComment];
|
return YES;
|
}
|
- (void)textFieldDidEndEditing:(UITextField *)textField{
|
[_tableView setScrollEnabled:YES];
|
_textField.placeholder=@"我也来说两句...";
|
}
|
|
#pragma mark 键盘隐藏
|
-(void)keyboardWillHide:(NSNotification *)notification{
|
_replyMessageView.frame=CGRectMake(0, _tableView.frame.size.height-50, self.view.frame.size.width, 50);
|
}
|
|
#pragma mark 键盘出现
|
-(void)keyboardWasShown:(NSNotification *)notification{
|
NSDictionary* info = [notification userInfo];
|
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;//得到键盘的高度
|
_replyMessageView.frame=CGRectMake(0, _tableView.frame.size.height-50-kbSize.height, self.view.frame.size.width, 50);
|
}
|
|
@end
|