//
|
// Mymessage.m
|
// BuWanVideo2.0
|
//
|
// Created by weikou2016 on 16/8/11.
|
// Copyright © 2016年 com.yeshi.buwansheque.ios. All rights reserved.
|
//
|
|
#import "MymessageContrlloer.h"
|
#import "HMSegmentedControl.h"
|
#import "UIImage+YTH.h"
|
#import "SystemMessageCell.h"
|
#import "ComentCell.h"
|
#import "LoggingViewController.h"
|
|
#define Systemcell @"SystemMessageCell"
|
#define ComentCel @"ComentCell"
|
|
@interface MymessageContrlloer ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate,UIGestureRecognizerDelegate>{
|
BOOL WhetherChooseSystemMessage; // NO我的消息 YES系统消息
|
|
HMSegmentedControl *segmentedControl;
|
UITableView*tableview;
|
|
UIImageView *tipPic; //消息为空的图片
|
UILabel *tipMessage; //消息为空的字
|
|
//数据源
|
NSMutableArray * Comment; //评论
|
NSMutableArray * SystemMessage; //系统消息
|
NSMutableArray * nowChoose; //当前数据源
|
|
//回复框
|
UIView *replyMessageView;
|
UITextField *textField;
|
|
int page;
|
int nowclick; //当前回复哪一个评论
|
}
|
|
@end
|
|
@implementation MymessageContrlloer
|
|
- (void)viewDidLoad {
|
[super viewDidLoad];
|
|
page=1;
|
WhetherChooseSystemMessage = YES;
|
[self initScene];
|
}
|
|
-(void)initScene{
|
//设置背景色
|
self.view.backgroundColor = kGlobalBackgroundColor;
|
|
//设置导航栏
|
[self createNavgationBar];
|
|
//创建选项
|
[self createSegmentedControl];
|
|
//创建列表
|
[self createTableView];
|
|
//没有数据时的提示视图
|
[self promptView];
|
|
//输入框视图
|
[self createReplyMessageView];
|
|
//获取系统消息
|
[self systemMessage];
|
}
|
|
-(void)createNavgationBar{
|
self.navigationItem.title = @"我的消息";
|
self.navigationController.navigationBar.titleTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:[UIColor whiteColor]};
|
|
//取消侧边栏消息提醒
|
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"HaveMessage"];
|
//通知视图刷新
|
[[NSNotificationCenter defaultCenter] postNotificationName:@"RELOAD_Message" object:nil];
|
|
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
|
[button setImage:[[UIImage imageNamed:@"详情页面返回"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
|
[button addTarget:self action:@selector(back:)
|
forControlEvents:UIControlEventTouchUpInside];
|
button.frame = CGRectMake(0, 0, 32, 32);
|
UIBarButtonItem *iconBarItem=[[UIBarButtonItem alloc] initWithCustomView:button];
|
self.navigationItem.leftBarButtonItem = iconBarItem;
|
}
|
|
-(void)createSegmentedControl{
|
CGFloat height = 64;
|
if (KIsiPhoneX) {
|
height = 84;
|
}
|
segmentedControl = [[HMSegmentedControl alloc] initWithSectionTitles:@[@"评论消息", @"系统消息"]];
|
[segmentedControl setFrame:CGRectMake(0, height, KScreenW, 48)];
|
segmentedControl.selectionIndicatorHeight = 4.0f;
|
segmentedControl.backgroundColor = KGlobalLightGreyColor_255;
|
segmentedControl.titleTextAttributes = @{NSForegroundColorAttributeName : kGlobalDeepGreyColor};
|
segmentedControl.selectionIndicatorColor = kGlobalYellowColor;
|
segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleTextWidthStripe;
|
segmentedControl.selectedSegmentIndex = 0;
|
segmentedControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
|
segmentedControl.shouldAnimateUserSelection = YES;
|
segmentedControl.titleTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:15]};
|
[segmentedControl addTarget:self action:@selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];
|
|
//选项下面的横线
|
UILabel *Line = [[UILabel alloc]initWithFrame:CGRectMake(0, 48, KScreenW, 1)];
|
Line.backgroundColor = kGlobaLineColor;
|
[segmentedControl addSubview:Line];
|
|
//选项中间的分割线
|
UILabel *Line1 = [[UILabel alloc]initWithFrame:CGRectMake(KScreenW / 2 - 0.5 , 10 ,1, 28)];
|
Line.backgroundColor = kGlobaLineColor;
|
[segmentedControl addSubview:Line1];
|
|
// [self.view addSubview:segmentedControl];
|
}
|
|
-(void)createTableView{
|
CGFloat height = 117;
|
if (KIsiPhoneX) {
|
height = 137;
|
}
|
tableview = [[UITableView alloc]initWithFrame:CGRectMake(10, 0 + 1, KScreenW - 20, KScreenH - height + segmentedControl.frame.size.height) style:UITableViewStyleGrouped];
|
tableview.showsVerticalScrollIndicator = NO;
|
tableview.backgroundColor = [UIColor clearColor];
|
tableview.delegate = self;
|
tableview.dataSource = self;
|
tableview.backgroundColor = kGlobalBackgroundColor;
|
tableview.contentInset = UIEdgeInsetsMake(5, 0, 0, 0);
|
|
[tableview registerNib:[UINib nibWithNibName:Systemcell bundle:nil] forCellReuseIdentifier:Systemcell];
|
[tableview registerNib:[UINib nibWithNibName:ComentCel bundle:nil] forCellReuseIdentifier:ComentCel];
|
|
tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headUpData)];
|
tableview.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footUpData)];
|
[tableview.mj_header beginRefreshing];
|
[self.view addSubview:tableview];
|
}
|
|
-(void)promptView{
|
//提示
|
tipPic = [UIImageView new]; // 176*225
|
tipPic.image = [UIImage imageNamed:@"评论为空"];
|
tipPic.frame = CGRectMake(KScreenW/2-44, KScreenH/2 - 80, 88, 112.5);
|
[self.view addSubview:tipPic];
|
|
tipMessage = [[UILabel alloc]initWithFrame:CGRectMake(0, 52, KScreenW, 1)];
|
tipMessage.frame = CGRectMake(KScreenW / 2 - 100, tipPic.frame.size.height + tipPic.frame.origin.y + 20, 200, 30);
|
tipMessage.textAlignment = NSTextAlignmentCenter;
|
tipMessage.font = [UIFont systemFontOfSize:18];
|
tipMessage.textColor = kGlobalLightGreyColor_210;
|
[self.view addSubview:tipMessage];
|
}
|
|
-(void)createReplyMessageView{
|
// 回复框
|
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, KScreenH, KScreenW, 50)];
|
replyMessageView.backgroundColor=[UIColor whiteColor];
|
//回复的输入框
|
textField=[[UITextField alloc] initWithFrame:CGRectMake(20, 10, KScreenW-90 , 30)];
|
textField.delegate=self;
|
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
|
textField.placeholder=@" 我也来说两句...";
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
selector:@selector(keyboardWillShown:)
|
name:UIKeyboardWillShowNotification object:nil];
|
//回复发送按钮
|
UIButton *sendButton=[[UIButton alloc] initWithFrame:CGRectMake(KScreenW-60, 10, 50, 30)]; // 50 * 30
|
[sendButton setBackgroundImage:[[UIImage imageNamed:@"发表"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]forState:UIControlStateNormal];
|
[sendButton setTintColor:[UIColor whiteColor]];
|
[sendButton setTitle:@"发送" forState:UIControlStateNormal];
|
[sendButton addTarget:self action:@selector(ClickSendComment) forControlEvents:UIControlEventTouchUpInside ];
|
[sendButton setTitleColor:kGlobalMainColor forState:UIControlStateNormal];
|
[replyMessageView addSubview:backimage];
|
[replyMessageView addSubview:textField];
|
[replyMessageView addSubview:sendButton];
|
[self.view addSubview:replyMessageView];
|
replyMessageView.hidden = YES;
|
|
UITapGestureRecognizer *TapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(touchview)];
|
TapGesture.delegate = self;
|
[self.view addGestureRecognizer:TapGesture];
|
|
}
|
|
-(void)systemMessage{
|
SystemMessage = [[NSMutableArray alloc]initWithContentsOfFile:MESSAGE];
|
NSMutableArray *temp = [NSMutableArray new];
|
for (int i = 0 ; i < SystemMessage.count ; ++i){
|
NSMutableDictionary *dic = SystemMessage[i];
|
NSString * cha = [self autoHeiWithString:[dic objectForKey:@"Content"] WithFont:15 WithWidth:KScreenW-40];
|
|
[dic setObject:cha forKey:@"cha"];
|
[temp addObject:dic];
|
}
|
SystemMessage = temp;
|
}
|
|
/**
|
* 底部刷新
|
*/
|
-(void)footUpData{
|
if(WhetherChooseSystemMessage){
|
[[YTHNetInterface startInterface]getMyCommentReplyWithUid:[[NSUserDefaults standardUserDefaults] objectForKey:@"LoginUid"] WithPage:[NSString stringWithFormat:@"%d",page] WithSystem:@"1" WithBlock:^(BOOL isSuccessful, id result, NSString *error) {
|
if(isSuccessful){
|
NSArray* newComment = [[result objectForKey:@"Data"]objectForKey:@"data"];
|
|
[Comment addObjectsFromArray:newComment];
|
if(!WhetherChooseSystemMessage){
|
nowChoose = Comment;
|
}
|
[self setheight];
|
[self reloadData];
|
page++;
|
}else{
|
NSLog(@"%@",error);
|
}
|
[tableview.mj_footer endRefreshing];
|
}];
|
}else{
|
[tableview.mj_footer endRefreshing];
|
}
|
|
}
|
|
/**
|
* 上部刷新
|
*/
|
-(void)headUpData{
|
if (WhetherChooseSystemMessage) {
|
page =1;
|
[[YTHNetInterface startInterface]getMyCommentReplyWithUid:[[NSUserDefaults standardUserDefaults] objectForKey:@"LoginUid"] WithPage:[NSString stringWithFormat:@"%d",page] WithSystem:@"1" WithBlock:^(BOOL isSuccessful, id result, NSString *error) {
|
|
if(isSuccessful){
|
|
Comment = [[result objectForKey:@"Data"]objectForKey:@"data"];
|
|
if(Comment.count > 0){
|
[self setheight];
|
if(!WhetherChooseSystemMessage){
|
nowChoose = Comment;
|
}
|
page++;
|
}
|
if (Comment.count < 20) {
|
tableview.mj_footer.hidden = YES;
|
}else{
|
tableview.mj_footer.hidden = NO;
|
}
|
[self reloadData];
|
}else{
|
NSLog(@"%@",error);
|
if ([error compare:@"似乎已断开与互联网的连接。"] == 0) {
|
[self autoDisappearAlertTime:1 msg:@"网络不可用,请检查网络"];
|
}
|
}
|
[tableview.mj_header endRefreshing];
|
}];
|
}else{
|
[tableview.mj_header endRefreshing];
|
}
|
}
|
|
/**
|
* 计算字符串的高度并传入到字典中
|
*/
|
-(void)setheight{
|
for(int i = 0 ; i < Comment.count ; ++i){
|
NSString *str1 = [Comment[i] objectForKey:@"Content"];
|
NSString *str2 = [[Comment[i] objectForKey:@"Comment"]objectForKey:@"Content"];
|
NSString * cha1 = [self autoHeiWithString:str1 WithFont:15 WithWidth:KScreenW-40];
|
NSString * cha2 = [self autoHeiWithString:str2 WithFont:15 WithWidth:KScreenW-40];
|
[Comment[i] setObject:cha1 forKey:@"mycomment"];
|
[Comment[i] setObject:cha2 forKey:@"othercomment"];
|
}
|
}
|
|
/**
|
* 对view的监听 点击会让键盘输入框消失
|
*/
|
-(void)touchview{
|
[self ReturnKeybiard];
|
}
|
|
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
|
if([NSStringFromClass(touch.view.class) isEqualToString:@"HMScrollView"]){
|
return NO;
|
}
|
return YES;
|
}
|
|
/**
|
* 刷新数据 并且判断显示是否有 没有消息的背景图
|
*/
|
-(void)reloadData{
|
[self doImage];
|
// if(nowChoose.count){
|
[tableview reloadData];
|
// }else{
|
// [self performSelector:@selector(reloadData) withObject:nil afterDelay:1];
|
// }
|
}
|
|
/*
|
* YES 显示
|
*/
|
-(void)doImage{
|
if(nowChoose.count == 0){
|
tipPic.hidden = NO;
|
tipMessage.hidden = NO;
|
if(WhetherChooseSystemMessage){
|
tipMessage.text = @"还没有系统消息";
|
}else{
|
tipMessage.text = @"还没有评论消息";
|
}
|
}else{
|
tipPic.hidden = YES;
|
tipMessage.hidden = YES;
|
}
|
[tableview reloadData];
|
}
|
|
#pragma mark - UITableViewDataSource - UITableViewDelegate
|
|
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
|
return 1;
|
}
|
|
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
|
return nowChoose.count;
|
}
|
|
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
|
return 5;
|
}
|
|
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
|
return 5;
|
}
|
|
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
|
|
if (WhetherChooseSystemMessage) { //右
|
NSDictionary *dic = [nowChoose objectAtIndex:indexPath.section];
|
float cha = ((NSString*)[dic objectForKey:@"cha"]).floatValue;
|
return 55+cha;
|
}else{ //左
|
NSDictionary *dic = Comment[indexPath.section];
|
|
NSString *commentStr1 = [dic objectForKey:@"Content"];//我的评论
|
CGSize commentSize1 = [commentStr1 boundingRectWithSize:CGSizeMake(KScreenW-23, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size;
|
|
NSString *commentStr2 = [[dic objectForKey:@"Comment"]objectForKey:@"Content"];//别人的回复
|
CGSize commentSize2 = [commentStr2 boundingRectWithSize:CGSizeMake(KScreenW-43, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]} context:nil].size;
|
|
return 145 + commentSize1.height + commentSize2.height;
|
}
|
}
|
|
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
|
|
if(WhetherChooseSystemMessage){ //系统的
|
SystemMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:Systemcell forIndexPath:indexPath];
|
NSDictionary *dic = [nowChoose objectAtIndex:indexPath.section];
|
//自适应数据接收处理
|
if(!cell.isauto){
|
cell.Message.text = (NSString*)[dic objectForKey:@"Content"];
|
float cha = ((NSString*)[dic objectForKey:@"cha"]).floatValue;
|
cell.frame = CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height + cha);
|
cell.Message.frame = CGRectMake(cell.Message.frame.origin.x, cell.Message.frame.origin.y, cell.Message.frame.size.width, cell.Message.frame.size.height+cha);
|
cell.isauto = YES;
|
}
|
//非自适应数据
|
NSString *time = [dic objectForKey:@"date"];
|
NSString *day = [time substringToIndex:10]; //年月日
|
NSString *hour = [time substringFromIndex:11];//小时分钟
|
cell.Time1.text = day;
|
cell.Time2.text = hour;
|
return cell;
|
}else{
|
ComentCell *cell = [tableView dequeueReusableCellWithIdentifier:ComentCel forIndexPath:indexPath];
|
|
NSDictionary *dic = Comment[indexPath.section];
|
|
NSString *commentStr1 = [dic objectForKey:@"Content"];//我的评论
|
CGSize commentSize1 = [commentStr1 boundingRectWithSize:CGSizeMake(KScreenW-16, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size;
|
|
NSString *commentStr2 = [[dic objectForKey:@"Comment"]objectForKey:@"Content"];//别人的回复
|
CGSize commentSize2 = [commentStr2 boundingRectWithSize:CGSizeMake(KScreenW-43, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]} context:nil].size;
|
|
cell.contentH1.constant=commentSize1.height;
|
cell.contentH2.constant=commentSize2.height;
|
|
cell.NowComment.text = commentStr1;
|
cell.CommentMessage.text = commentStr2;
|
|
cell.HeadImage.layer.cornerRadius = cell.HeadImage.frame.size.width/2;
|
cell.HeadImage.layer.masksToBounds = YES;
|
[cell.HeadImage setYthImageWithURL:[[dic objectForKey:@"User"] objectForKey:@"Portrait"] placeholderImage:[UIImage imageNamed:@"用户默认"]];
|
cell.Name.text = [[dic objectForKey:@"User"] objectForKey:@"Nickname"];
|
|
cell.WhoComment.text = [NSString stringWithFormat:@"%@:",[[NSUserDefaults standardUserDefaults] objectForKey:@"QQUserName"]];
|
cell.ComeFrom.text = [NSString stringWithFormat:@"来源:%@",[[[dic objectForKey:@"Comment"] objectForKey:@"Video"]objectForKey:@"Name"]];
|
|
cell.Time.text = [[YTHNetInterface startInterface]getDate:[dic objectForKey:@"Createtime"]];
|
|
cell.Replacemessage.tag =indexPath.section;
|
[cell.Replacemessage addTarget:self action:@selector(ClickReplace:) forControlEvents:UIControlEventTouchUpInside];
|
|
return cell;
|
}
|
return nil;
|
}
|
|
#pragma mark 选择器切换时触发的函数
|
- (void)segmentedControlChangedValue:(HMSegmentedControl *)segmentedControls {
|
NSLog(@"Selected index %ld", (long)segmentedControls.selectedSegmentIndex);
|
// if(segmentedControls.selectedSegmentIndex==0){
|
// WhetherChooseSystemMessage=NO;
|
// nowChoose = Comment;
|
// }else{
|
// WhetherChooseSystemMessage=YES;
|
// nowChoose = SystemMessage;
|
// }
|
// //图片文字是否显示
|
// [self doImage];
|
//
|
// //重新获取数据源
|
// [tableview.mj_header beginRefreshing];
|
// //重新加载数据
|
// [tableview reloadData];
|
}
|
|
|
|
/**
|
* 计算字符串的高度
|
*/
|
-(NSString*)autoHeiWithString:(NSString*)str WithFont:(int)font_i WithWidth:(float)width{
|
UILabel *tempLabel = [UILabel new];
|
tempLabel.text = str;
|
tempLabel.font = [UIFont systemFontOfSize:font_i];
|
tempLabel.numberOfLines = 0;
|
CGSize size = [tempLabel sizeThatFits:CGSizeMake(width, MAXFLOAT)];
|
|
if(font_i == 12){
|
return [NSString stringWithFormat:@"%f",size.height - 14.33]; //减去当前字号的大小 由于XIB写了一排
|
}else if(font_i == 15){
|
return [NSString stringWithFormat:@"%f",size.height - 18];
|
}
|
return nil;
|
}
|
|
|
-(void)back:(id)sender{
|
[self.navigationController popViewControllerAnimated:YES];
|
}
|
|
/**
|
* 点击出现键盘
|
*
|
* @return
|
*/
|
-(void)ClickReplace:(UIButton*)sender{
|
nowclick = (int)sender.tag;
|
replyMessageView.hidden = NO;
|
[textField becomeFirstResponder];
|
}
|
|
#pragma mark 键盘出现
|
-(void)keyboardWillShown:(NSNotification *)notification{
|
NSDictionary* info = [notification userInfo];
|
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;//得到键盘的高度
|
replyMessageView.frame=CGRectMake(0, KScreenH-50-kbSize.height, KScreenW, 50);
|
textField.clearButtonMode = UITextFieldViewModeAlways;
|
}
|
|
//返回按钮
|
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
|
[self ReturnKeybiard];
|
return YES;
|
}
|
|
//收回键盘
|
-(void)ReturnKeybiard{
|
[textField resignFirstResponder];
|
replyMessageView.frame=CGRectMake(0, KScreenH-50, KScreenW, 50);
|
replyMessageView.hidden = YES;
|
}
|
|
#pragma mark 点击发送评论
|
-(void)ClickSendComment{
|
[self ReturnKeybiard];
|
NSDictionary *dic = Comment[nowclick]; //Id
|
|
[[YTHNetInterface startInterface]getreplayCommentWithUid:[[NSUserDefaults standardUserDefaults] objectForKey:@"LoginUid"] WithcommentReplayId:[dic objectForKey:@"Id"] WithCommentId:[[dic objectForKey:@"Comment"]objectForKey:@"Id"] WithContent:textField.text WithSystem:@"1" WithBlock:^(BOOL isSuccessful, id result, NSString *error) {
|
if(isSuccessful){
|
NSLog(@"回复成功");
|
}else{
|
NSLog(@"%@",error);
|
}
|
}];
|
}
|
@end
|