AllCommentVC.m 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. //
  2. // AllCommentVC.m
  3. // jiaPei
  4. //
  5. // Created by apple on 15/11/23.
  6. // Copyright (c) 2015年 JCZ. All rights reserved.
  7. //
  8. #import "AllCommentVC.h"
  9. @interface AllCommentVC ()<UITableViewDataSource,UITableViewDelegate,UITextViewDelegate>
  10. {
  11. UIView* tfContainv;
  12. UITextView* tf;
  13. UIButton* postBtn;
  14. CGFloat tfcH;
  15. CGFloat tfcY;
  16. }
  17. @property(nonatomic,strong)NSArray* comments;
  18. @end
  19. @implementation AllCommentVC
  20. {
  21. UITableView* myTableView;
  22. }
  23. - (void)viewDidLoad {
  24. [super viewDidLoad];
  25. [self myInit];
  26. [self getAllComments];
  27. }
  28. - (void)didReceiveMemoryWarning {
  29. [super didReceiveMemoryWarning];
  30. }
  31. -(void)viewWillAppear:(BOOL)animated{
  32. [super viewWillAppear:animated];
  33. [[NSNotificationCenter defaultCenter] addObserver:self
  34. selector:@selector(keyboardWillShow:)
  35. name:UIKeyboardWillShowNotification
  36. object:nil];
  37. [[NSNotificationCenter defaultCenter] addObserver:self
  38. selector:@selector(keyboardWillHide:)
  39. name:UIKeyboardWillHideNotification
  40. object:nil];
  41. }
  42. -(void)viewWillDisappear:(BOOL)animated
  43. {
  44. [super viewWillDisappear:animated];
  45. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
  46. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
  47. }
  48. #pragma mark -
  49. -(void)myInit
  50. {
  51. if (myDelegate.isNightMode) {
  52. [self.view setBackgroundColor:nightColor];
  53. }else{
  54. [self.view setBackgroundColor:[UIColor whiteColor]];
  55. }
  56. [self configNavigationBar];
  57. [self setTitle:@"试题分析"];
  58. myTableView = [[UITableView alloc] initWithFrame:kFrame];
  59. myTableView.height -= 50 + kNavOffSet + kSafeAreaBottomHeight;
  60. [self.view addSubview:myTableView];
  61. [myTableView setBackgroundColor:[UIColor clearColor]];
  62. [myTableView setDataSource:self];
  63. [myTableView setDelegate:self];
  64. //UIScrollViewKeyboardDismissModeInteractive 这种是tableview拉到键盘的地方 键盘开始随手指移动 get起来
  65. myTableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
  66. tfcH = 50;
  67. CGFloat bd = 10;
  68. CGFloat btnw = 50;
  69. tfcY = kSize.height - tfcH - kNavOffSet - kSafeAreaBottomHeight;
  70. //NSLog(@"--%f----%f----%f---%f",tfcY,[[UIApplication sharedApplication] statusBarFrame].size.height,self.navigationController.navigationBar.frame.size.height,kSize.height);
  71. tfContainv = [[UIView alloc] initWithFrame:CGRectMake(0, tfcY, kSize.width, tfcH)];
  72. //灰色会效果更好点 似乎
  73. [tfContainv setBackgroundColor:[UIColor lightGrayColor]];
  74. [self.view addSubview:tfContainv];
  75. tf = [[UITextView alloc] initWithFrame:CGRectMake(bd, (tfcH - 25)/2, kSize.width-bd*3 - btnw, 25)];
  76. [tf setFont:[UIFont scaleSize:NormalFont]];
  77. [tf setFont:[UIFont scaleSize:15]];
  78. [tfContainv addSubview:tf];
  79. [tf setDelegate:self];
  80. // [tfContainv addSelfViewWithRect:CGRectMake(bd, tfcH - 1, kSize.width-bd*3 - btnw-4, 1) Color:defGreen];
  81. UIButton* btn =[[UIButton alloc] initWithFrame:CGRectMake(kSize.width - btnw - bd, (tfcH - 30)/2, btnw, 30)];
  82. [btn setTitle:@"发送" textColor:backGroundColor font:NormalFont fotState:UIControlStateNormal];
  83. [btn addTarget:self action:@selector(userEndEdit) forControlEvents:UIControlEventTouchUpInside];
  84. [btn setBackgroundColor:[UIColor grayColor]];
  85. [btn.layer setCornerRadius:5];
  86. [btn.layer setMasksToBounds:YES];
  87. postBtn = btn;
  88. [tfContainv addSubview: btn];
  89. }
  90. - (void)saveQuestionComment
  91. {
  92. if (![Util connectedToNetWork]) {
  93. showMsgUnconnect();
  94. return;
  95. }
  96. //NSLog(@"com->%@,qid->%@",tf.text,_quest._id);
  97. NSMutableArray *arr=[NSMutableArray array];
  98. [arr property:_quest._id forKey:@"qId"];
  99. [arr property:tf.text forKey:@"desc"];
  100. if (myDelegate.isLogin) {
  101. NSString *str = defUser.userName;
  102. if (str && str.length!=0) {
  103. [arr property:defUser.userName forKey:@"name"];
  104. }else{
  105. [arr property:defUser.userDict[@"nickName"] forKey:@"name"];
  106. }
  107. [arr property:defUser.userDict[@"id"] forKey:@"userId"];
  108. [arr property:@"1" forKey:@"type"];
  109. }else{
  110. [arr property:@"驾考学员" forKey:@"name"];
  111. [arr property:@"2" forKey:@"type"];
  112. [arr property:@"" forKey:@"userId"];
  113. }
  114. NSString* method = @"saveQuestionComment";
  115. [MBProgressHUD showLoadToView:self.view];
  116. [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary *dict) {
  117. [MBProgressHUD hideHUDForView:self.view];
  118. //NSLog(@"同步请求返回的结果:%@",dict);
  119. if (dict && [dict[@"code"] isEqualToString:@"0"]) {
  120. ShowMsg(@"评论成功");
  121. tf.text = nil;
  122. [self getAllComments];
  123. }else{
  124. ShowMsg(dict[@"body"]);
  125. }
  126. }];
  127. }
  128. -(void)getAllComments
  129. {
  130. if (![Util connectedToNetWork]) {
  131. showMsgUnconnect();
  132. return;
  133. }
  134. NSMutableArray *arr=[NSMutableArray array];
  135. if ([defUser.car_type isEqualToString:@"2"]) {
  136. [arr property:_quest._id forKey:@"id"];
  137. } else {
  138. [arr property:[NSString stringWithFormat:@"%@",[NSNumber numberWithInteger:_ydtQuestionModel.ID]] forKey:@"id"];
  139. }
  140. [arr property:@"" forKey:@"isPage"];
  141. [arr property:@"" forKey:@"pageSize"];
  142. [arr property:@"" forKey:@"currentPage"];
  143. NSString* method = @"getQuestionComments";
  144. [MBProgressHUD showLoadToView:self.view];
  145. [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary *dict) {
  146. [MBProgressHUD hideHUDForView:self.view];
  147. if (!dict) {
  148. ShowMsgFailed();
  149. return;
  150. }
  151. if ([dict[@"code"] isEqualToString:@"1"])
  152. {
  153. ShowMsg(dict[@"body"]);
  154. return;
  155. }
  156. _comments = dict[@"body"];
  157. if (![dict[@"body"] isKindOfClass:[NSArray class]]) {
  158. _comments = @[];
  159. }
  160. if (_comments.count < 1) {
  161. ShowMsg(@"暂无相关评论");
  162. }
  163. [myTableView reloadData];
  164. }];
  165. }
  166. #pragma mark -
  167. -(void)userEndEdit
  168. {
  169. if ([tf isFirstResponder]) {
  170. [tf resignFirstResponder];
  171. }
  172. [UIView animateWithDuration:.25 animations:^{
  173. tfContainv.frame = CGRectMake(0, tfcY, kSize.width, tfcH);
  174. }];
  175. NSCharacterSet *whitespace=[NSCharacterSet whitespaceAndNewlineCharacterSet];
  176. NSString* str=[tf.text stringByTrimmingCharactersInSet:whitespace];
  177. if (str.length < 1) {
  178. return;
  179. }
  180. [self saveQuestionComment];
  181. }
  182. - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
  183. {
  184. // NSCharacterSet *whitespace=[NSCharacterSet whitespaceAndNewlineCharacterSet];
  185. // NSString* str=[tf.text stringByTrimmingCharactersInSet:whitespace];
  186. //这样来做应该会更准确
  187. NSMutableString * changedString=[[NSMutableString alloc]initWithString:textView.text];
  188. [changedString replaceCharactersInRange:range withString:text];
  189. if (changedString.length < 1) {
  190. [postBtn setEnabled:NO];
  191. [postBtn setBackgroundColor:[UIColor grayColor]];
  192. }else{
  193. [postBtn setEnabled:YES];
  194. [postBtn setBackgroundColor:defGreen];
  195. }
  196. if ([text isEqualToString:@"\n"]){
  197. [self userEndEdit];
  198. return NO;
  199. //这里返回NO,就代表return键值失效,即页面上按下return,不会出现换行,如果为yes,则输入页面会换行
  200. }
  201. return YES;
  202. }
  203. - (void)keyboardWillShow:(NSNotification *)aNotification
  204. {
  205. NSDictionary *userInfo = [aNotification userInfo];
  206. NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
  207. CGRect keyboardRect = [aValue CGRectValue];
  208. CGFloat kbH = keyboardRect.size.height;
  209. [UIView animateWithDuration:.25 animations:^{
  210. tfContainv.frame = CGRectMake(0, tfcY - kbH + kSafeAreaBottomHeight, kSize.width, tfcH);
  211. }];
  212. }
  213. - (void)keyboardWillHide:(NSNotification *)aNotification
  214. {
  215. if ([tf isFirstResponder]) {
  216. [tf resignFirstResponder];
  217. }
  218. [UIView animateWithDuration:.25 animations:^{
  219. tfContainv.frame = CGRectMake(0, tfcY, kSize.width, tfcH);
  220. }];
  221. }
  222. #pragma mark -
  223. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  224. {
  225. return _comments.count ;
  226. }
  227. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  228. {
  229. NSDictionary *dic = _comments[indexPath.row];
  230. return 110 + [dic[@"QC_DESC"] heightForWid:(kSize.width - 80) Font:15];
  231. }
  232. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  233. {
  234. CommentCell* cell = [CommentCell cellForTableView:tableView Style:CommentCellStyleQuestion];
  235. if(indexPath.row < _comments.count){
  236. [cell setCommentDic:_comments[indexPath.row]];
  237. }
  238. return cell;
  239. }
  240. -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
  241. return [UIView new];
  242. }
  243. -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
  244. {
  245. return .1;
  246. }
  247. @end