admin
2023-04-21 0b3a4aaf99ea251bc8e27b96115288f0988fcffe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
//
//  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