// #import "TopicVC.h" #import "CommunityCell.h" #import "TopicComCell.h" #import "STButton.h" #import "LoginViewController.h" @interface TopicVC () { /**输入框的父视图 */ UIView* tfContainv; UITextView* tf; /**“发送”按钮 */ UIButton* postBtn; UITableView* myTableView; /**仅仅是某话题的详情 */ NSDictionary* model; /**某话题的评论列表列表 */ NSArray* models; /**全屏的阴影按钮 */ UIView *shadowView; /**下方的点赞按钮。保存引用,以便于修改 */ STButton* favBtn; /**收藏按钮。不是点赞 */ STButton* storeBtn; /**下方的评论按钮。保存引用,以便于修改 */ STButton* comBtn; /**显示有多少条评论,以及评论人数。 */ UIView* footer; UILabel* lblNames,*lblComCnt; /**当用户点击cell的评论按钮时,传来一个用户ID。需要保存在这里。 */ NSString* replayUser; //判断是否是话题拥有者 BOOL isUser; //输入框内容 NSString *commentString; } @end @implementation TopicVC -(void)dealloc{ [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; } /**考虑到这个VC的表结构的问题。先去设置导航右按钮 */ - (void)viewDidLoad { [super viewDidLoad]; [self myInit]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } -(void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [self getTopicInfo]; if (_textViewActive) { _textViewActive = NO; [tf becomeFirstResponder]; } } #pragma mark - -(void)myInit { [self setTitle:@"话题详情"]; [self.view setBackgroundColor:[UIColor whiteColor]]; if (_isNotification == NO) { //正常状态 [self configNavigationBar]; } else { //推送状态 要将根视图变回去 UIBarButtonItem* backBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"question_pre_checked_icon.png"] style:UIBarButtonItemStylePlain target:self action:@selector(dismissNavgation)]; [backBtn setTintColor:defGreen]; self.navigationController.navigationBar.translucent = NO; [self.navigationItem setLeftBarButtonItem:backBtn]; } /**配置导航右按钮 */ UIBarButtonItem *backBbi = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"community3.png"] style:UIBarButtonItemStylePlain target:self action:@selector(btnClick:)]; [backBbi setTintColor:defGreen]; [backBbi setTag:2]; [self.navigationItem setRightBarButtonItem:backBbi]; CGFloat x,y,w,h,bd; NSInteger tag = 0; //配置bottombar CGFloat barH = 65; x = 0; y = 0; w = kSize.width; h = kSize.height - kNavOffSet - kSafeAreaBottomHeight - barH; UITableView* tv = [[UITableView alloc] initWithFrame:CGRectMake(x, y, w, h) style:UITableViewStyleGrouped]; tv.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kSize.width, .1)]; [self.view addSubview:tv]; [tv setBackgroundColor:[UIColor whiteColor]]; tv.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag; myTableView = tv; [myTableView setDelegate:self]; [myTableView setDataSource:self]; myTableView.estimatedSectionHeaderHeight = 0; [tv setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]]; if ([tv respondsToSelector:@selector(setSeparatorInset:)]) { [tv setSeparatorInset:UIEdgeInsetsZero]; } if ([tv respondsToSelector:@selector(setLayoutMargins:)]) { [tv setLayoutMargins:UIEdgeInsetsZero]; } y+=h; h = barH; tfContainv = [[UIView alloc] initWithFrame:CGRectMake(0, y, w, h)]; //NSLog(@"%@",NSStringFromCGRect(tfContainv.frame)); [tfContainv setBackgroundColor:backGroundColor]; [self.view addSubview:tfContainv]; UIView* vi = [[UIView alloc] initWithFrame:CGRectMake(0, y, w, h)]; [vi setBackgroundColor:backGroundColor]; [self.view addSubview:vi]; // vi.hidden= YES; w = 50; bd = 10; tf = [[UITextView alloc] initWithFrame:CGRectMake(bd, (50 - 25)/2, kSize.width-bd*3 - w, 25)]; [tf setFont:[UIFont scaleSize:NormalFont]]; [tf setFont:[UIFont scaleSize:15]]; [tfContainv addSubview:tf]; [tf setDelegate:self]; // [tfContainv addSelfViewWithRect:CGRectMake(bd, 25/2+25+1, kSize.width-bd*3 - w-4, 1) Color:defGreen]; //注册通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; UIButton* btn =[[UIButton alloc] initWithFrame:CGRectMake(kSize.width - w - bd, (50 - 30)/2, w, 30)]; [btn setTitle:@"发送" textColor:backGroundColor font:NormalFont fotState:UIControlStateNormal]; [btn addTarget:self action:@selector(userEndEdit) forControlEvents:UIControlEventTouchUpInside]; [btn setBackgroundColor:[UIColor grayColor]]; [btn.layer setCornerRadius:5]; [btn.layer setMasksToBounds:YES]; postBtn = btn; [tfContainv addSubview: btn]; w = h*1.3; bd = (kSize.width - w*2)/5.0; x = bd*2; STButton* stBtn = [[STButton alloc] initWithFrame:CGRectMake(x, 0, w, h)]; [stBtn setStyle:1]; [stBtn setImage:[UIImage imageNamed:@"community0.png"] withTitle:@"" Font:SmallFont forState:UIControlStateNormal]; [stBtn setImage:[UIImage imageNamed:@"community1.png"] forState:UIControlStateSelected]; [stBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; [stBtn setTitleColor:contentTextColor forState:UIControlStateNormal]; [vi addSubview:stBtn]; favBtn = stBtn; [stBtn setTag:tag++];//0 x +=bd+w; stBtn = [[STButton alloc] initWithFrame:CGRectMake(x, 0, w, h)]; [stBtn setStyle:1]; [stBtn setImage:[UIImage imageNamed:@"community2.png"] withTitle:@"评论" Font:SmallFont forState:UIControlStateNormal]; [stBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; [stBtn setTitleColor:contentTextColor forState:UIControlStateNormal]; [vi addSubview:stBtn]; comBtn = stBtn; [stBtn setTag:1]; /**tableView创建 */ vi = [[UIView alloc] init]; [vi setBackgroundColor:[UIColor whiteColor]]; footer = vi; UILabel* label; label = [[UILabel alloc] init]; [vi addSubview:label]; [label setTextColor:defGreen]; [label setFont:[UIFont scaleSize:13]]; label.numberOfLines = 0; lblNames = label; label = [[UILabel alloc] init]; [vi addSubview:label]; lblComCnt = label; [label setTextColor:contentTextColor]; [label setFont:[UIFont scaleSize:13]]; } -(void)dismissNavgation { //推送过来 返回主页面 [myDelegate gotoLoad]; } -(void)btnClick:(UIButton*)sender { NSInteger tag = sender.tag; if (0 == tag ||5 == tag ||6 == tag) { if (!myDelegate.isLogin) { [LoginViewController loginFromVC:self]; return; } } if (0 == tag) { //话题点赞 int num = [model[@"POINTNUM"] intValue] - [model[@"ISPOINT"] intValue]; if (sender.selected) { [sender setTitle:[NSString stringWithFormat:@"赞(%d)",num] forState:UIControlStateNormal]; [self deleteTopicCommentPoints]; }else{ [sender setTitle:[NSString stringWithFormat:@"赞(%d)",num+1] forState:UIControlStateNormal]; [self uploadTopicCommentPoints]; } sender.selected = !sender.selected; } if (1 == tag) { if (!myDelegate.isLogin) { LoginViewController* vc = [[LoginViewController alloc] init]; vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; vc.modalPresentationStyle = UIModalPresentationFullScreen; [self presentViewController:vc animated:YES completion:nil]; }else{ //展示输入框。 [tf becomeFirstResponder]; } } if ( 2 == tag) { [tf resignFirstResponder]; if (shadowView) { //慢慢消失 [self hideMenu]; }else{ //慢慢出现 shadowView = [[UIView alloc] initWithFrame:CGRectMake(0, -80, kSize.width, kSize.height)]; shadowView.backgroundColor = [UIColor colorWithWhite:.1 alpha:0.42]; [self.view addSubview:shadowView]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideMenu)]; [shadowView addGestureRecognizer:tap]; NSArray* titles; NSArray* images; if(isUser) { titles= @[@"删除",@"收藏"]; images = @[@"community6.png",@"Topic_img03.png"]; }else{ titles= @[@"举报",@"收藏",@"屏蔽此人",@"屏蔽此条"]; images = @[@"Topic_img02.png",@"Topic_img03.png",@"Topic_img02.png",@"Topic_img02.png"]; } CGFloat w = kSize.width / titles.count; for (int i = 0; i%@",models); [lblComCnt setText:[NSString stringWithFormat:@"%d条评论",(int)models.count]]; lblComCnt.frame = CGRectMake(10, lblNames.bottom, kSize.width, 30); [lblComCnt addViewWithRect:CGRectMake(10, lblNames.bottom, kSize.width-20, 1)]; [comBtn setTitle:[NSString stringWithFormat:@"评论(%d)",(int)models.count] forState:UIControlStateNormal]; footer.frame = CGRectMake(0, 0, kSize.width, lblComCnt.bottom); //要先改footer再刷新表 要不然 返回的区尾尺寸不对 [myTableView reloadData]; }]; } /**获取点赞用户列表 */ -(void)getTopicPoints { if (![Util connectedToNetWork]) { showMsgUnconnect(); return; } NSMutableArray *arr=[NSMutableArray array]; [arr addPro:@"topicId" Value:_topicId]; NSString* method = @"getTopicPoints"; [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary * dict) { if (!dict) { ShowMsg(@"获取点赞列表失败!"); lblNames.frame = CGRectMake(10, 0, kSize.width - 60, 30); [self getTopicComments]; return; } if ( [dict[@"code"] isEqualToString:@"1"]) { ShowMsg(dict[@"body"]); lblNames.frame = CGRectMake(10, 0, kSize.width - 60, 30); [self getTopicComments]; return; } NSArray *array = dict[@"body"]; //NSLog(@"获取话题赞数---->%@",array); NSMutableString* str = [NSMutableString string]; for (int i =0;i < array.count;i++) { if ( i < array.count - 1) { [str appendFormat:@"%@,",[array[i] objectForKey:@"NICKNAME"]]; }else{ [str appendFormat:@"%@ 等%d人赞过",[array[i] objectForKey:@"NICKNAME"],(int)array.count]; } } if (array.count < 1) { [lblNames setText:@"还没人赞过呢"]; }else{ [lblNames setText:str]; } //为了更好看。高度30是随意设置的。真正的高度是在下面一行计算的。 lblNames.frame = CGRectMake(10, 0, kSize.width - 60, 30); lblNames.height = [str heightForWid:(kSize.width - 60) Font:13]+15; [self getTopicComments]; }]; } /** */ -(void)uploadTopicReportInfo:(NSString*)type { if (![Util connectedToNetWork]) { showMsgUnconnect(); return; } // if (!_topicId) { // _topicId = _preDict[@"ID"]; // } NSMutableArray *arr = [NSMutableArray array]; [arr addPro:@"topicId" Value:_topicId]; [arr addPro:@"type" Value:type]; [arr addPro:@"user" Value:defUser.userDict[@"id"]]; NSString* method = @"uploadTopicReportInfo"; [MBProgressHUD showLoadToView:self.view]; [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary * dict) { [MBProgressHUD hideHUDForView:self.view]; if (!dict) { ShowMsgFailed(); return ; } if ( [dict[@"code"] isEqualToString:@"1"]) { ShowMsg(dict[@"body"]); return ; } ShowMsgSuc(); }]; } /**收藏某个话题 */ -(void)uploadTopicFav { if (![Util connectedToNetWork]) { showMsgUnconnect(); return; } if (!_topicId) { _topicId = @"1"; } NSMutableArray *arr = [NSMutableArray array]; [arr addPro:@"topicId" Value:_topicId]; [arr addPro:@"user" Value:defUser.userDict[@"id"]]; NSString* method = @"uploadTopicFav"; [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary * dict) { //NSLog(@"收藏---->%@",dict); if (!dict) { ShowMsgFailed(); return; } if ( [dict[@"code"] isEqualToString:@"1"]) { ShowMsg(dict[@"body"]); return; }else{ ShowMsgSuc(); [model setValue:@"1" forKey:@"ISFAV"]; } }]; } -(void)deleteMyTopicFav { if (![Util connectedToNetWork]) { showMsgUnconnect(); return; } if (!_topicId) { _topicId = @"1"; } NSMutableArray *arr = [NSMutableArray array]; [arr addPro:@"topicId" Value:_topicId]; [arr addPro:@"user" Value:defUser.userDict[@"id"]]; NSString* method = @"deleteMyTopicFav"; [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary * dict) { if (!dict) { ShowMsgFailed(); return; } if ( [dict[@"code"] isEqualToString:@"1"]) { ShowMsg(dict[@"body"]); return; }else{ ShowMsgSuc(); [model setValue:@"0" forKey:@"ISFAV"]; } }]; } /**type 1 对话题评论 2 对评论进行回复 */ -(void)uploadTopicComment { if (![Util connectedToNetWork]) { showMsgUnconnect(); return; } if (!_topicId) { _topicId = @"1"; } NSString* type; if (!replayUser) { type = @"1"; replayUser = @""; }else{ type = @"2"; } NSMutableArray * arr = [NSMutableArray array]; [arr addPro:@"content" Value:commentString]; [arr addPro:@"user" Value:defUser.userDict[@"id"]]; [arr addPro:@"topicId" Value:_topicId]; [arr addPro:@"type" Value:type]; [arr addPro:@"replyUser" Value:replayUser]; //NSLog(@"defUser.userDict-->%@---------replyUser-->%@",defUser.userDict[@"NICKNAME"],replayUser); NSString* method = @"uploadTopicComment"; [MBProgressHUD showLoadToView:self.view]; [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary * dict) { [MBProgressHUD hideHUDForView:self.view]; if (!dict) { ShowMsgFailed(); return; } if ( [dict[@"code"] isEqualToString:@"1"]) { ShowMsg(dict[@"body"]); return; } //要刷新显示 replayUser = @""; //这里没必要再查话题了 耗流量 [self getTopicComments]; }]; } -(void)uploadTopicCommentPoints { if (![Util connectedToNetWork]) { showMsgUnconnect(); return; } NSMutableArray* arr = [NSMutableArray array]; [arr addPro:@"user" Value:defUser.userDict[@"id"]]; [arr addPro:@"topicId" Value:model[@"ID"]]; NSString* method = @"uploadTopicCommentPoints"; [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary * root) { //这种处理等若没写 if (!root) { ShowMsgFailed(); return; } }]; } -(void)deleteTopicCommentPoints { if (![Util connectedToNetWork]) { showMsgUnconnect(); return; } NSMutableArray* arr = [NSMutableArray array]; [arr addPro:@"user" Value:defUser.userDict[@"id"]]; [arr addPro:@"topicId" Value:_topicId]; NSString* method = @"deleteTopicCommentPoints"; [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary * root) { if (!root) { ShowMsgFailed(); return; } }]; } -(void)deleteTopicInfo { if (![Util connectedToNetWork]) { showMsgUnconnect(); return; } NSMutableArray* arr = [NSMutableArray array]; [arr addPro:@"id" Value:_topicId]; NSString* method = @"deleteTopicInfo"; [MBProgressHUD showLoadToView:self.view]; [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary * root) { [MBProgressHUD hideHUDForView:self.view]; if (!root) { ShowMsgFailed(); return; } ShowMsgSuc(); [self.navigationController popViewControllerAnimated:YES]; }]; } #pragma mark - -(void)gotoCommentsByName:(NSString *)name { if (!myDelegate.isLogin) { [LoginViewController loginFromVC:self]; }else{ replayUser = name; [tf becomeFirstResponder]; } } -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { if (4 == buttonIndex) { return; }else{ NSString* type = [NSString stringWithFormat:@"%d",(int)buttonIndex+19]; [self uploadTopicReportInfo:type]; } } -(void)userEndEdit { [tf resignFirstResponder]; commentString = tf.text; if (commentString.length < 1) { return; } tf.text = @""; [self uploadTopicComment]; } - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { NSCharacterSet *whitespace=[NSCharacterSet whitespaceAndNewlineCharacterSet]; NSString* str=[tf.text stringByTrimmingCharactersInSet:whitespace]; if (str.length < 1) { [postBtn setEnabled:NO]; [postBtn setBackgroundColor:[UIColor grayColor]]; }else{ [postBtn setEnabled:YES]; [postBtn setBackgroundColor:defGreen]; } if ([text isEqualToString:@"\n"]){ [self userEndEdit]; return NO; //这里返回NO,就代表return键值失效,即页面上按下return,不会出现换行,如果为yes,则输入页面会换行 } return YES; } /**本来不想用通知中心。改用textView.delegate。 可是。为了动态获取键盘高度,只能用这个了。 */ - (void)keyboardWillShow:(NSNotification *)aNotification { NSDictionary *userInfo = [aNotification userInfo]; NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]; CGRect keyboardRect = [aValue CGRectValue]; CGFloat kbH = keyboardRect.size.height; [UIView animateWithDuration:.25 animations:^{ tfContainv.frame = CGRectMake(0, kSize.height - kNavOffSet - 50 - kbH, kSize.width, 50); }]; } - (void)keyboardWillHide:(NSNotification *)aNotification { [UIView animateWithDuration:.25 animations:^{ tfContainv.frame = CGRectMake(0, kSize.height, kSize.width, 50); }]; } #pragma mark - -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { if (0 == section) { return footer; }else{ return [UIView new]; } } -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { if (0 == section) { return footer.height; }else{ return .1; } } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return .1; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (0 == indexPath.section) { return communityCellHeight(model, 1); }else{ return TopicComCellHeight(models[indexPath.row]); } } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (0 == section) { return 1; }else{ return models.count; } } -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // return 2; return (models && models.count > 0) + 1; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if ( 0 == indexPath.section) { CommunityCell* cell = [CommunityCell cellForTableView:tableView Style:1]; cell.block = ^{ [tf resignFirstResponder]; }; [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; [cell setModel:model]; [cell setShowTag:YES]; //用于查看学车日记 [cell setDelegate:self]; return cell; } if (models.count > indexPath.row) { TopicComCell* cell = [TopicComCell cellForTableView:tableView ]; cell.isAdopt = isUser; [cell setModel:models[indexPath.row]]; [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; [cell setDelegate:self]; return cell; } UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"qiguaide"]; return cell; } //表在滑动的时候触发的方法 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { if ([cell respondsToSelector:@selector(setSeparatorInset:)]) { [cell setSeparatorInset:UIEdgeInsetsZero]; } if ([cell respondsToSelector:@selector(setLayoutMargins:)]) { [cell setLayoutMargins:UIEdgeInsetsZero]; } } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [tf resignFirstResponder]; } @end