CommentCell.m 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. //
  2. // CommentCell.m
  3. // test12
  4. //
  5. // Created by apple on 15/11/22.
  6. // Copyright (c) 2015年 JCZ. All rights reserved.
  7. //
  8. #import "CommentCell.h"
  9. CommentCellStyle cellStyle;
  10. @implementation CommentCell
  11. - (void)awakeFromNib
  12. {
  13. [super awakeFromNib];
  14. }
  15. - (void)setSelected:(BOOL)selected animated:(BOOL)animated
  16. {
  17. [super setSelected:NO animated:animated];
  18. }
  19. -(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  20. {
  21. if ([reuseIdentifier isEqualToString:@"school"])
  22. {
  23. cellStyle = CommentCellStyleSchool;
  24. }
  25. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  26. if (self) {
  27. self.imageView.layer.masksToBounds = YES;
  28. UIButton* btn = [[UIButton alloc] init];
  29. [btn setImage:[UIImage imageNamed:@"question_approve_checked_icon.png"] forState:UIControlStateSelected];
  30. [btn setImage:[UIImage imageNamed:@"question_approve_icon.png"] forState:UIControlStateNormal];
  31. [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
  32. [self.contentView addSubview:btn];
  33. [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
  34. _upBtn = btn;
  35. [btn.imageView setContentMode:UIViewContentModeScaleAspectFit];
  36. UIImageView* iv = [[UIImageView alloc] init];
  37. [iv setContentMode:UIViewContentModeScaleAspectFit];
  38. [self.contentView addSubview:iv];
  39. honorIv = iv;
  40. UILabel* lb = [[UILabel alloc] init];
  41. [self.contentView addSubview:lb];
  42. [lb setTextColor:contentTextColor];
  43. celDateLbl = lb;
  44. [celDateLbl setFont:[UIFont scaleSize:12]];
  45. [self.textLabel setTextColor:kTitleColor];
  46. [self.detailTextLabel setTextColor:subTitleColor];
  47. }
  48. [self setSelectionStyle:UITableViewCellSelectionStyleNone];
  49. return self;
  50. }
  51. +(CommentCell*)cellForTableView:(UITableView*)tableView Style:(CommentCellStyle )style
  52. {
  53. cellStyle = style;
  54. NSString* cellid;
  55. /**如果2个风格的cell,需要区分,可以在这设置
  56. */
  57. if (CommentCellStyleQuestion == style) {
  58. cellid= @"CommentCellIdentifier";
  59. }else{
  60. cellid= @"CommentCellIdentifier";
  61. }
  62. CommentCell* cell = [tableView dequeueReusableCellWithIdentifier:cellid];
  63. if (!cell) {
  64. cell = [[CommentCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellid];
  65. [cell setBackgroundColor:[UIColor clearColor]];
  66. }
  67. return cell;
  68. }
  69. -(void)setCommentDic:(NSDictionary *)commentDic
  70. {
  71. _commentDic = commentDic;
  72. [self.imageView sd_setImageWithURL:[NSURL URLWithString:commentDic[@"PHOTO"]] placeholderImage:randomPortrait()];
  73. [self.textLabel setText:commentDic[@"QC_NAME"]];
  74. [self.detailTextLabel setText:commentDic[@"QC_DESC"]];
  75. [_upBtn setTitle:commentDic[@"QC_UP_NUM"] forState:UIControlStateNormal];
  76. [_upBtn setSelected:NO];//这里似乎没有字段来判定是否已被自己点过赞
  77. NSString* str = commentDic[@"QC_CRDATE"];
  78. if (str && str.length >16) {
  79. [celDateLbl setText:[str substringToIndex:16]];
  80. }else{
  81. [celDateLbl setText:str];
  82. }
  83. }
  84. -(void)setDict:(NSDictionary *)dict
  85. {
  86. _dict = dict;
  87. [_upBtn setTitle:dict[@"POINTNUM"] forState:UIControlStateNormal];
  88. [_upBtn setSelected:NO];
  89. if ([dict[@"ISPOINT"] isEqualToString:@"1"]) {
  90. [_upBtn setSelected:YES];
  91. }
  92. self.imageView.contentMode = UIViewContentModeScaleAspectFit;
  93. self.imageView.clipsToBounds = YES;
  94. if (![dict[@"NAME"] isEqualToString:@""] && ![dict[@"NAME"] isEqualToString:@"暂无相关点评~"])
  95. {
  96. _upBtn.hidden = NO;
  97. NSString *path = [Tools imageStringWithPhotoString:dict[@"PHOTO"] HeadImgString:dict[@"HEADIMG"] Type:2];
  98. if (path.length < 1) {
  99. path = nil;
  100. }
  101. [self.imageView sd_setImageWithURL:[NSURL URLWithString:path] placeholderImage:[UIImage imageNamed:[NSString stringWithFormat:@"portrait%d.png",arc4random()%16 + 1]]];
  102. }else{
  103. _upBtn.hidden = YES;
  104. }
  105. [self.textLabel setText:dict[@"NAME"]];
  106. [self.detailTextLabel setText:dict[@"PJCONTENT"]];
  107. NSString* str = dict[@"PJSJ"];
  108. if (str && str.length >11) {
  109. [celDateLbl setText:[str substringToIndex:11]];
  110. }else{
  111. [celDateLbl setText:str];
  112. }
  113. }
  114. /**每次cell重用时,这里都会调用一次。
  115. 切记不要再这里创建视图。
  116. */
  117. -(void)layoutSubviews
  118. {
  119. [super layoutSubviews];
  120. CGFloat font = 15;
  121. [self.detailTextLabel setNumberOfLines:0];
  122. [self.detailTextLabel setFont:[UIFont scaleSize:font]];
  123. CGFloat aWid = self.width;
  124. CGFloat x,w,h,y,bd;
  125. x = 5;
  126. w = h = 55;
  127. y = bd = 10;
  128. [self.imageView setFrame:CGRectMake(x, y, w, h)];
  129. self.imageView.layer.cornerRadius = h/2.0;
  130. if (cellStyle == CommentCellStyleQuestion)
  131. {
  132. y += bd;
  133. x = x + w + bd;
  134. h = 25;
  135. [self.textLabel setFrame:CGRectMake(x, y, 260, h)];
  136. y = y + h + bd;
  137. w = aWid - x - bd;
  138. h = [self.detailTextLabel.text heightForWid:w Font:font];
  139. //现在主要是根据desc的内容动态设置宽高了
  140. [self.detailTextLabel setFrame:CGRectMake(x, y, w, h)];
  141. y = y+h+bd;
  142. h = 30;
  143. [celDateLbl setFrame:CGRectMake(x, y, 260, h)];
  144. [_upBtn setFrame:CGRectMake(aWid - 100, y, 100, h)];
  145. [honorIv setFrame:CGRectMake(aWid - 55, 5, 55, 55)];
  146. }else{
  147. y += bd;
  148. x = x + w + bd;
  149. h = 25;
  150. [self.textLabel setFrame:CGRectMake(x, y, 180, h)];
  151. y = y + h + bd;
  152. w = aWid - x - bd;
  153. h = [self.detailTextLabel.text heightForWid:w Font:font];
  154. if (h < 40)
  155. {
  156. h = 40;
  157. }
  158. //现在主要是根据desc的内容动态设置宽高了
  159. [self.detailTextLabel setFrame:CGRectMake(x, y, w, h)];
  160. y = y+h+bd;
  161. h = 30;
  162. [celDateLbl setFrame:CGRectMake(x, y, 260, h)];
  163. [_upBtn setFrame:CGRectMake(aWid - 100, y, 100, h)];
  164. [honorIv setFrame:CGRectMake(aWid - 55, 5, 55, 55)];
  165. }
  166. }
  167. -(void)btnClick:(UIButton*)sender
  168. {
  169. if ([_VCName isEqualToString:@"SearchComment"]) {
  170. //代表是驾校的页面
  171. return;
  172. }
  173. [self UpLoadApproveComId];
  174. }
  175. - (void)UpLoadApproveComId
  176. {
  177. if (![Util connectedToNetWork]) {
  178. showMsgUnconnect();
  179. return;
  180. }
  181. NSMutableArray *arr=[NSMutableArray array];
  182. NSString *comId = _commentDic ? _commentDic[@"QC_ID"] : _dict[@"ID"];
  183. [arr property:comId forKey:@"cid"];
  184. [arr property:@"1" forKey:@"type"];
  185. if (myDelegate.isLogin) {
  186. [arr property:defUser.userDict[@"id"] forKey:@"userId"];
  187. }else{
  188. [arr property:@"驾考学员" forKey:@"userId"];
  189. }
  190. NSString* method = @"updateQuestionUpOrDown";
  191. [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary *dict) {
  192. if (!dict) {
  193. ShowMsg(@"操作失败");
  194. return;
  195. }
  196. if ( [dict[@"code"] isEqualToString:@"1"]) {
  197. ShowMsg(dict[@"body"]);
  198. [_upBtn setSelected:YES];
  199. return;
  200. }
  201. NSString *comUpNum = _commentDic ? _commentDic[@"QC_UP_NUM"] : _dict[@"POINTNUM"];
  202. comUpNum = [NSString stringWithFormat:@"%d",comUpNum.intValue+1];
  203. [_upBtn setTitle:comUpNum forState:UIControlStateNormal];
  204. [_upBtn setSelected:YES];
  205. }];
  206. }
  207. @end