HomeDetailCommentCell.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. //
  2. // HomeDetailCommentCell.m
  3. // TheoryNetwork
  4. //
  5. // Created by tederen on 2019/9/27.
  6. // Copyright © 2019 tederen. All rights reserved.
  7. //
  8. #import "HomeDetailCommentCell.h"
  9. #import "CommentModel.h"
  10. #import "CommentSubModel.h"
  11. #import "ComentSubCell.h"
  12. #import "NSDate+JKExtension.h"
  13. @interface HomeDetailCommentCell ()<UITableViewDelegate,UITableViewDataSource>
  14. @property (nonatomic, strong) UIImageView *portraitImgVi;
  15. @property (nonatomic, strong) UILabel *nameLabel;
  16. @property (nonatomic, strong) UILabel *floorLabel;
  17. @property (nonatomic, strong) UILabel *timeLabel;
  18. @property (nonatomic, strong) UILabel *contentLabel;
  19. @property (nonatomic, strong) TDButton *replyButton;
  20. @property (nonatomic,strong) TDButton *zanBtn;
  21. @property (nonatomic, strong) TDTableView * myTableView;
  22. @property (nonatomic, strong) TDButton *BTN;
  23. @property (nonatomic, strong) UILabel *repaylab;
  24. @property (nonatomic, strong) NSMutableArray<CommentSubModel*>* repayArrData;
  25. @property (nonatomic, strong) CommentModel*currentModel;
  26. @end
  27. @implementation HomeDetailCommentCell
  28. - (void)clickIcon:( CommentModel*)currentModel{
  29. NSLog(@"点击评论头像的ID%ld",currentModel.UserId);
  30. if ([self.delegate respondsToSelector:@selector(didClickIconLookup:)]) {
  31. [self.delegate didClickIconLookup:currentModel];
  32. }
  33. }
  34. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  35. {
  36. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  37. if (self) {
  38. // Initialization code
  39. [self addSubview:self.portraitImgVi];
  40. [self addSubview:self.nameLabel];
  41. [self addSubview:self.floorLabel];
  42. [self addSubview:self.zanBtn];
  43. [self addSubview:self.timeLabel];
  44. [self addSubview:self.contentLabel];
  45. [self addSubview:self.repaylab];
  46. [self addSubview:self.replyButton];
  47. [self addSubview:self.myTableView];
  48. }
  49. return self;
  50. }
  51. #pragma mark - publicMethod
  52. - (void)loadCommentData:(CommentModel *)model {
  53. self.currentModel = model;
  54. UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(21,0, kGXScreenWidth-42,0.5)];
  55. lineView.backgroundColor = UIColorHex(E5E5E5);
  56. [self addSubview:lineView];
  57. // 头像
  58. if (!ISEmptyString(model.AvatarUrl)) {
  59. [_portraitImgVi sd_setImageWithURL:[NSURL URLWithString:model.AvatarUrl] placeholderImage:IMG(@"img_placeHolder")];
  60. } else {
  61. [_portraitImgVi setImage:IMG(@"zhujun2")];
  62. }
  63. _portraitImgVi.frame = CGRectMake(20,15, 36, 36);
  64. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(id _Nonnull sender) {
  65. [self clickIcon:self.currentModel];
  66. }];;
  67. self.portraitImgVi.tag = model.UserId;
  68. self.portraitImgVi.userInteractionEnabled = YES;
  69. [self.portraitImgVi addGestureRecognizer:tap];
  70. // 名字
  71. _nameLabel.text = model.Name;
  72. CGSize nameSize = [_nameLabel sizeThatFits:CGSizeZero];
  73. _nameLabel.frame = CGRectMake(68, 15, nameSize.width, nameSize.height);
  74. //几楼
  75. _floorLabel.text = model.FloorCount;
  76. CGSize floorSize = [_floorLabel sizeThatFits:CGSizeZero];
  77. _floorLabel.frame = CGRectMake(68, 40,floorSize.width, floorSize.height);
  78. // 时间
  79. CGSize timeSize;
  80. if (!ISEmptyString(model.CreatedTime)) {
  81. _timeLabel.text = model.CreatedTime;
  82. //如果当前时间大于大于24小时+ (nullable NSDate *)dateWithString:(NSString *)dateString format:(NSString *)format;
  83. NSDate *date = [NSDate dateWithString:model.CreatedTime format:@"yyyy-MM-dd HH:mm:ss"];
  84. NSTimeInterval time = [[NSDate date] by_timeIntervalWithDate:date];
  85. NSTimeInterval dayTime = 60 * 60 * 24;
  86. if (time < dayTime) {
  87. //相差多少秒
  88. CGFloat hous = time / (60 * 60);
  89. if (hous < 1) {
  90. //分钟
  91. CGFloat mm = time / 60;
  92. if (mm < 1) {
  93. _timeLabel.text = @"刚刚";
  94. }else {
  95. _timeLabel.text = [NSString stringWithFormat:@"%.0f分钟前",mm];
  96. }
  97. }else {
  98. _timeLabel.text = [NSString stringWithFormat:@"%.0f小时前",hous];
  99. }
  100. }else {
  101. //判断时间是否今年
  102. //分成2段
  103. NSArray *time = [model.CreatedTime componentsSeparatedByString:@" "];
  104. //拆分时分秒
  105. NSArray *yyyyHHmm = [[time firstObject] componentsSeparatedByString:@"-"];
  106. //判断是否大于今年
  107. NSDate *date =[NSDate date];//简书 FlyElephant
  108. NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
  109. [formatter setDateFormat:@"yyyy"];
  110. NSInteger currentYear = [[formatter stringFromDate:date] integerValue];
  111. if ([[yyyyHHmm firstObject] integerValue] < currentYear) {
  112. _timeLabel.text = [NSString stringWithFormat:@"%@年%@月%@日",yyyyHHmm[0],yyyyHHmm[1],yyyyHHmm[2]];
  113. }else {
  114. NSArray *tmpTime = [[time lastObject] componentsSeparatedByString:@":"];
  115. _timeLabel.text = [NSString stringWithFormat:@"%@-%@ %@:%@",yyyyHHmm[1],yyyyHHmm[2],tmpTime[0],tmpTime[1]];
  116. }
  117. }
  118. timeSize = [_timeLabel sizeThatFits:CGSizeZero];
  119. _timeLabel.frame = CGRectMake(CGRectGetMaxX(_floorLabel.frame)+15,40,timeSize.width, timeSize.height);
  120. }
  121. timeSize = [_timeLabel sizeThatFits:CGSizeZero];
  122. _timeLabel.frame = CGRectMake(CGRectGetMaxX(_floorLabel.frame)+15,40,timeSize.width, timeSize.height);
  123. _zanBtn.frame = CGRectMake(kGXScreenWidth -50, 16,40, 40); //⚠️此处布局
  124. [_zanBtn setImageEdgeInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
  125. [_zanBtn addTarget:self action:@selector(zanHander:) forControlEvents:UIControlEventTouchUpInside];
  126. [_zanBtn setImage:IMG(@"zan_yes") forState:UIControlStateSelected];
  127. [_zanBtn setImage:IMG(@"zan_no") forState:UIControlStateNormal];
  128. _zanBtn.selected = model.IsLaud;
  129. NSLog(@"%@",model.Content);
  130. if (!ISEmptyString(model.Content)) {
  131. [_replyButton setTitle: model.Content forState:UIControlStateNormal];
  132. [[_replyButton titleLabel] setFont:[UIFont systemFontOfSize:17.5]];
  133. [[_replyButton titleLabel] setNumberOfLines:0];
  134. [ZYCTool setLabel:[_replyButton titleLabel] withSpace:10.f withFont:[_replyButton titleLabel].font setLineSpace:0 setTextSpace:2.f];
  135. [_replyButton setTitleColor:UIColorHex(636363) forState:UIControlStateNormal];
  136. CGSize replyButtonSize = [[_replyButton titleLabel] sizeThatFits:CGSizeMake(kGXScreenWidth-68-21, MAXFLOAT)];
  137. _replyButton.frame = CGRectMake(68,64,replyButtonSize.width, replyButtonSize.height);
  138. }else{
  139. _replyButton.frame = CGRectMake(68,64,0,0);
  140. }
  141. if (model.CommentReplyResults.count == 0){
  142. return;
  143. }
  144. NSInteger tabviewHeight = 0;
  145. for (CommentSubModel *subModel in model.CommentReplyResults) {
  146. tabviewHeight += subModel.getCellHeight;
  147. }
  148. self.myTableView.frame = CGRectMake(68,CGRectGetMaxY(_replyButton.frame) + 10,kGXScreenWidth - 68 -21,tabviewHeight);
  149. [self.myTableView reloadData];
  150. self.repayArrData = [NSMutableArray arrayWithArray:model.CommentReplyResults];
  151. [self.myTableView setNeedsLayout];
  152. if (model.CommentReplyResults.count == 0) {
  153. self.myTableView.frame = CGRectMake(68, CGRectGetMaxY(_replyButton.frame) +10,0,0);
  154. }
  155. }
  156. #pragma mark -UITableViewDelegate,
  157. #pragma mark - UITableViewDataSource
  158. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  159. return self.repayArrData.count;
  160. }
  161. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  162. CommentSubModel *model = self.repayArrData[indexPath.row];
  163. NSString *cellIdentifier = NSStringFromSelector(_cmd);
  164. ComentSubCell *detailCommentCell = (ComentSubCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
  165. if (!detailCommentCell) {
  166. detailCommentCell = [[ComentSubCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
  167. }
  168. [detailCommentCell loadCommentSubModelData:model];
  169. detailCommentCell.selectionStyle = UITableViewCellSelectionStyleNone;
  170. return detailCommentCell;
  171. }
  172. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  173. return self.repayArrData[indexPath.row].getCellHeight;
  174. }
  175. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  176. if ([self.delegate respondsToSelector:@selector(didClickRepayCommentComment:)]) {
  177. [self.delegate didClickRepayCommentComment:self.repayArrData[indexPath.row]];
  178. }
  179. }
  180. - (void)oldLayOutZhushi{
  181. // _nameLabel.text = model.name;
  182. // CGSize nameSize = [_nameLabel sizeThatFits:CGSizeZero];
  183. // [_nameLabel mas_updateConstraints:^(MASConstraintMaker *make) {
  184. // make.size.mas_equalTo(nameSize);
  185. // }];
  186. //
  187. // if (!ISEmptyString(model.portraitUrl)) {
  188. // [_portraitImgVi sd_setImageWithURL:[NSURL URLWithString:IMGURL(model.portraitUrl)] placeholderImage:IMG(@"img_placeHolder")];
  189. //
  190. // } else {
  191. // [_portraitImgVi setImage:IMG(@"zhujun")];//img_placeHolder
  192. // }
  193. //
  194. // _floorLabel.text = [NSString stringWithFormat:@"第%@楼", model.floor];
  195. // CGSize floorSize = [_floorLabel sizeThatFits:CGSizeZero];
  196. // [_floorLabel mas_updateConstraints:^(MASConstraintMaker *make) {
  197. // make.height.mas_equalTo(floorSize.height);
  198. // make.width.mas_equalTo(floorSize.width);
  199. // }];
  200. // [_zanBtn setSelected:YES];
  201. // if (!ISEmptyString(model.time)) {
  202. // _timeLabel.text = model.time;
  203. // CGSize timeSize = [_timeLabel sizeThatFits:CGSizeZero];
  204. // [_timeLabel mas_updateConstraints:^(MASConstraintMaker *make) {
  205. // make.size.mas_equalTo(timeSize);
  206. // }];
  207. // }
  208. //
  209. // _contentLabel.text = model.content;
  210. // CGSize contentSize = [_contentLabel sizeThatFits:CGSizeMake(kGXScreenWidth-65-15, MAXFLOAT)];
  211. // [_contentLabel mas_updateConstraints:^(MASConstraintMaker *make) {
  212. // make.size.mas_equalTo(contentSize);
  213. // }];
  214. //
  215. // if (model.replyTimes.intValue) {
  216. // self.replyButton.hidden = NO;
  217. // NSString *titleString = [NSString stringWithFormat:@"%@等人共%@条回复>", model.replyName, model.replyTimes];
  218. // [_replyButton setTitle:titleString forState:UIControlStateNormal];
  219. // [_replyButton setTitleColor:UIColorHex(1682DA) forState:UIControlStateNormal];
  220. // [[_replyButton titleLabel] setFont:[UIFont systemFontOfSize:15.f]];
  221. // [_replyButton mas_remakeConstraints:^(MASConstraintMaker *make) {
  222. // make.left.equalTo(self.contentView).offset(65.f);
  223. // make.top.equalTo(self.contentLabel.mas_bottom).offset(10.f);
  224. // // make.right.equalTo(self.contentView).offset(-15.f);
  225. // make.height.mas_equalTo(42.f);
  226. // make.width.mas_equalTo(kGXScreenWidth-65-15);
  227. // }];
  228. // } else {
  229. // self.replyButton.hidden = YES;
  230. // }
  231. }
  232. - (void)replyAction:(TDButton *)sender {
  233. if (self.touchAction) {
  234. self.touchAction();
  235. }
  236. if ([self.delegate respondsToSelector:@selector(didClickRepayComment:) ]){
  237. [self.delegate didClickRepayComment:self.currentModel];
  238. }
  239. }
  240. - (void)zanHander:(UIButton *)button {
  241. button.selected = !button.selected;
  242. if (self.touchZan) {
  243. self.touchZan();
  244. }
  245. if ([self.delegate respondsToSelector:@selector(didClickZanComment:withStatus:)]) {
  246. [self.delegate didClickZanComment:self.currentModel withStatus:button.selected];
  247. }
  248. }
  249. #pragma mark - setter/getter
  250. - (UIImageView *)portraitImgVi {
  251. if (!_portraitImgVi) {
  252. _portraitImgVi = [[UIImageView alloc] init];
  253. _portraitImgVi.layer.cornerRadius = 5.f;
  254. _portraitImgVi.layer.masksToBounds = YES;
  255. }
  256. return _portraitImgVi;
  257. }
  258. - (UILabel *)nameLabel {
  259. if (!_nameLabel) {
  260. _nameLabel = [UILabel new];
  261. _nameLabel.font = [UIFont systemFontOfSize:15.f];
  262. _nameLabel.textColor = k6;
  263. _nameLabel.text = @"占位";
  264. }
  265. return _nameLabel;
  266. }
  267. - (UILabel *)floorLabel {
  268. if (!_floorLabel) {
  269. _floorLabel = [UILabel new];
  270. _floorLabel.font = [UIFont systemFontOfSize:14.f];
  271. _floorLabel.textColor = k9;
  272. _floorLabel.text = @"占位";
  273. }
  274. return _floorLabel;
  275. }
  276. - (UILabel *)timeLabel {
  277. if (!_timeLabel) {
  278. _timeLabel = [UILabel new];
  279. _timeLabel.font = [UIFont systemFontOfSize:14.f];
  280. _timeLabel.textColor = UIColorHex(bbbbbb);
  281. _timeLabel.text = @"占位";
  282. }
  283. return _timeLabel;
  284. }
  285. - (UILabel *)contentLabel {
  286. if (!_contentLabel) {
  287. _contentLabel = [UILabel new];
  288. _contentLabel.numberOfLines = 0;
  289. _contentLabel.font = [UIFont systemFontOfSize:15.f];
  290. _contentLabel.textColor = k6;
  291. _contentLabel.text = @"占位";
  292. }
  293. return _contentLabel;
  294. }
  295. - (TDButton *)replyButton {
  296. if (!_replyButton) {
  297. _replyButton = [[TDButton alloc] init];
  298. // _replyButton.backgroundColor = UIColorHex(F6F6F6);
  299. // _replyButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  300. // _replyButton.contentEdgeInsets = UIEdgeInsetsMake(0, 15, 0, -15);
  301. [_replyButton addTarget:self action:@selector(replyAction:) forControlEvents:UIControlEventTouchUpInside];
  302. }
  303. return _replyButton;
  304. }
  305. - (TDButton *)zanBtn{
  306. if (!_zanBtn) {
  307. _zanBtn = [[TDButton alloc] init];
  308. [_zanBtn setImage:IMG(@"zan_yes") forState:UIControlStateSelected];
  309. [_zanBtn setImage:IMG(@"zan_no") forState:UIControlStateNormal];
  310. [_zanBtn setCurrentButtonHotSize:CGSizeZero];
  311. [_zanBtn addTarget:self action:@selector(zanHander:) forControlEvents:UIControlEventTouchUpInside];
  312. }
  313. return _zanBtn;
  314. }
  315. - (UILabel *)repaylab{
  316. if (!_repaylab) {
  317. _repaylab = [UILabel new];
  318. }
  319. return _repaylab;
  320. }
  321. - (CommentModel *)currentModel{
  322. if (!_currentModel) {
  323. _currentModel = [[CommentModel alloc] init];
  324. }
  325. return _currentModel;
  326. }
  327. - (TDTableView *)myTableView{
  328. if (!_myTableView) {
  329. _myTableView = [[TDTableView alloc]init];
  330. _myTableView.delegate = self;
  331. _myTableView.dataSource = self;
  332. }
  333. return _myTableView;
  334. }
  335. - (NSMutableArray<CommentSubModel *> *)repayArrData{
  336. if (!_repayArrData) {
  337. _repayArrData = [NSMutableArray array];
  338. }
  339. return _repayArrData;
  340. }
  341. @end