// // HomeDetailCommentCell.m // TheoryNetwork // // Created by tederen on 2019/9/27. // Copyright © 2019 tederen. All rights reserved. // #import "HomeDetailCommentCell.h" #import "CommentModel.h" #import "CommentSubModel.h" #import "ComentSubCell.h" #import "NSDate+JKExtension.h" @interface HomeDetailCommentCell () @property (nonatomic, strong) UIImageView *portraitImgVi; @property (nonatomic, strong) UILabel *nameLabel; @property (nonatomic, strong) UILabel *floorLabel; @property (nonatomic, strong) UILabel *timeLabel; @property (nonatomic, strong) UILabel *contentLabel; @property (nonatomic, strong) TDButton *replyButton; @property (nonatomic,strong) TDButton *zanBtn; @property (nonatomic, strong) TDTableView * myTableView; @property (nonatomic, strong) TDButton *BTN; @property (nonatomic, strong) UILabel *repaylab; @property (nonatomic, strong) NSMutableArray* repayArrData; @property (nonatomic, strong) CommentModel*currentModel; @end @implementation HomeDetailCommentCell - (void)clickIcon:( CommentModel*)currentModel{ NSLog(@"点击评论头像的ID%ld",currentModel.UserId); if ([self.delegate respondsToSelector:@selector(didClickIconLookup:)]) { [self.delegate didClickIconLookup:currentModel]; } } - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // Initialization code [self addSubview:self.portraitImgVi]; [self addSubview:self.nameLabel]; [self addSubview:self.floorLabel]; [self addSubview:self.zanBtn]; [self addSubview:self.timeLabel]; [self addSubview:self.contentLabel]; [self addSubview:self.repaylab]; [self addSubview:self.replyButton]; [self addSubview:self.myTableView]; } return self; } #pragma mark - publicMethod - (void)loadCommentData:(CommentModel *)model { self.currentModel = model; UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(21,0, kGXScreenWidth-42,0.5)]; lineView.backgroundColor = UIColorHex(E5E5E5); [self addSubview:lineView]; // 头像 if (!ISEmptyString(model.AvatarUrl)) { [_portraitImgVi sd_setImageWithURL:[NSURL URLWithString:model.AvatarUrl] placeholderImage:IMG(@"img_placeHolder")]; } else { [_portraitImgVi setImage:IMG(@"zhujun2")]; } _portraitImgVi.frame = CGRectMake(20,15, 36, 36); UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(id _Nonnull sender) { [self clickIcon:self.currentModel]; }];; self.portraitImgVi.tag = model.UserId; self.portraitImgVi.userInteractionEnabled = YES; [self.portraitImgVi addGestureRecognizer:tap]; // 名字 _nameLabel.text = model.Name; CGSize nameSize = [_nameLabel sizeThatFits:CGSizeZero]; _nameLabel.frame = CGRectMake(68, 15, nameSize.width, nameSize.height); //几楼 _floorLabel.text = model.FloorCount; CGSize floorSize = [_floorLabel sizeThatFits:CGSizeZero]; _floorLabel.frame = CGRectMake(68, 40,floorSize.width, floorSize.height); // 时间 CGSize timeSize; if (!ISEmptyString(model.CreatedTime)) { _timeLabel.text = model.CreatedTime; //如果当前时间大于大于24小时+ (nullable NSDate *)dateWithString:(NSString *)dateString format:(NSString *)format; NSDate *date = [NSDate dateWithString:model.CreatedTime format:@"yyyy-MM-dd HH:mm:ss"]; NSTimeInterval time = [[NSDate date] by_timeIntervalWithDate:date]; NSTimeInterval dayTime = 60 * 60 * 24; if (time < dayTime) { //相差多少秒 CGFloat hous = time / (60 * 60); if (hous < 1) { //分钟 CGFloat mm = time / 60; if (mm < 1) { _timeLabel.text = @"刚刚"; }else { _timeLabel.text = [NSString stringWithFormat:@"%.0f分钟前",mm]; } }else { _timeLabel.text = [NSString stringWithFormat:@"%.0f小时前",hous]; } }else { //判断时间是否今年 //分成2段 NSArray *time = [model.CreatedTime componentsSeparatedByString:@" "]; //拆分时分秒 NSArray *yyyyHHmm = [[time firstObject] componentsSeparatedByString:@"-"]; //判断是否大于今年 NSDate *date =[NSDate date];//简书 FlyElephant NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; [formatter setDateFormat:@"yyyy"]; NSInteger currentYear = [[formatter stringFromDate:date] integerValue]; if ([[yyyyHHmm firstObject] integerValue] < currentYear) { _timeLabel.text = [NSString stringWithFormat:@"%@年%@月%@日",yyyyHHmm[0],yyyyHHmm[1],yyyyHHmm[2]]; }else { NSArray *tmpTime = [[time lastObject] componentsSeparatedByString:@":"]; _timeLabel.text = [NSString stringWithFormat:@"%@-%@ %@:%@",yyyyHHmm[1],yyyyHHmm[2],tmpTime[0],tmpTime[1]]; } } timeSize = [_timeLabel sizeThatFits:CGSizeZero]; _timeLabel.frame = CGRectMake(CGRectGetMaxX(_floorLabel.frame)+15,40,timeSize.width, timeSize.height); } timeSize = [_timeLabel sizeThatFits:CGSizeZero]; _timeLabel.frame = CGRectMake(CGRectGetMaxX(_floorLabel.frame)+15,40,timeSize.width, timeSize.height); _zanBtn.frame = CGRectMake(kGXScreenWidth -50, 16,40, 40); //⚠️此处布局 [_zanBtn setImageEdgeInsets:UIEdgeInsetsMake(5, 5, 5, 5)]; [_zanBtn addTarget:self action:@selector(zanHander:) forControlEvents:UIControlEventTouchUpInside]; [_zanBtn setImage:IMG(@"zan_yes") forState:UIControlStateSelected]; [_zanBtn setImage:IMG(@"zan_no") forState:UIControlStateNormal]; _zanBtn.selected = model.IsLaud; NSLog(@"%@",model.Content); if (!ISEmptyString(model.Content)) { [_replyButton setTitle: model.Content forState:UIControlStateNormal]; [[_replyButton titleLabel] setFont:[UIFont systemFontOfSize:17.5]]; [[_replyButton titleLabel] setNumberOfLines:0]; [ZYCTool setLabel:[_replyButton titleLabel] withSpace:10.f withFont:[_replyButton titleLabel].font setLineSpace:0 setTextSpace:2.f]; [_replyButton setTitleColor:UIColorHex(636363) forState:UIControlStateNormal]; CGSize replyButtonSize = [[_replyButton titleLabel] sizeThatFits:CGSizeMake(kGXScreenWidth-68-21, MAXFLOAT)]; _replyButton.frame = CGRectMake(68,64,replyButtonSize.width, replyButtonSize.height); }else{ _replyButton.frame = CGRectMake(68,64,0,0); } if (model.CommentReplyResults.count == 0){ return; } NSInteger tabviewHeight = 0; for (CommentSubModel *subModel in model.CommentReplyResults) { tabviewHeight += subModel.getCellHeight; } self.myTableView.frame = CGRectMake(68,CGRectGetMaxY(_replyButton.frame) + 10,kGXScreenWidth - 68 -21,tabviewHeight); [self.myTableView reloadData]; self.repayArrData = [NSMutableArray arrayWithArray:model.CommentReplyResults]; [self.myTableView setNeedsLayout]; if (model.CommentReplyResults.count == 0) { self.myTableView.frame = CGRectMake(68, CGRectGetMaxY(_replyButton.frame) +10,0,0); } } #pragma mark -UITableViewDelegate, #pragma mark - UITableViewDataSource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.repayArrData.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ CommentSubModel *model = self.repayArrData[indexPath.row]; NSString *cellIdentifier = NSStringFromSelector(_cmd); ComentSubCell *detailCommentCell = (ComentSubCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (!detailCommentCell) { detailCommentCell = [[ComentSubCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; } [detailCommentCell loadCommentSubModelData:model]; detailCommentCell.selectionStyle = UITableViewCellSelectionStyleNone; return detailCommentCell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return self.repayArrData[indexPath.row].getCellHeight; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ if ([self.delegate respondsToSelector:@selector(didClickRepayCommentComment:)]) { [self.delegate didClickRepayCommentComment:self.repayArrData[indexPath.row]]; } } - (void)oldLayOutZhushi{ // _nameLabel.text = model.name; // CGSize nameSize = [_nameLabel sizeThatFits:CGSizeZero]; // [_nameLabel mas_updateConstraints:^(MASConstraintMaker *make) { // make.size.mas_equalTo(nameSize); // }]; // // if (!ISEmptyString(model.portraitUrl)) { // [_portraitImgVi sd_setImageWithURL:[NSURL URLWithString:IMGURL(model.portraitUrl)] placeholderImage:IMG(@"img_placeHolder")]; // // } else { // [_portraitImgVi setImage:IMG(@"zhujun")];//img_placeHolder // } // // _floorLabel.text = [NSString stringWithFormat:@"第%@楼", model.floor]; // CGSize floorSize = [_floorLabel sizeThatFits:CGSizeZero]; // [_floorLabel mas_updateConstraints:^(MASConstraintMaker *make) { // make.height.mas_equalTo(floorSize.height); // make.width.mas_equalTo(floorSize.width); // }]; // [_zanBtn setSelected:YES]; // if (!ISEmptyString(model.time)) { // _timeLabel.text = model.time; // CGSize timeSize = [_timeLabel sizeThatFits:CGSizeZero]; // [_timeLabel mas_updateConstraints:^(MASConstraintMaker *make) { // make.size.mas_equalTo(timeSize); // }]; // } // // _contentLabel.text = model.content; // CGSize contentSize = [_contentLabel sizeThatFits:CGSizeMake(kGXScreenWidth-65-15, MAXFLOAT)]; // [_contentLabel mas_updateConstraints:^(MASConstraintMaker *make) { // make.size.mas_equalTo(contentSize); // }]; // // if (model.replyTimes.intValue) { // self.replyButton.hidden = NO; // NSString *titleString = [NSString stringWithFormat:@"%@等人共%@条回复>", model.replyName, model.replyTimes]; // [_replyButton setTitle:titleString forState:UIControlStateNormal]; // [_replyButton setTitleColor:UIColorHex(1682DA) forState:UIControlStateNormal]; // [[_replyButton titleLabel] setFont:[UIFont systemFontOfSize:15.f]]; // [_replyButton mas_remakeConstraints:^(MASConstraintMaker *make) { // make.left.equalTo(self.contentView).offset(65.f); // make.top.equalTo(self.contentLabel.mas_bottom).offset(10.f); // // make.right.equalTo(self.contentView).offset(-15.f); // make.height.mas_equalTo(42.f); // make.width.mas_equalTo(kGXScreenWidth-65-15); // }]; // } else { // self.replyButton.hidden = YES; // } } - (void)replyAction:(TDButton *)sender { if (self.touchAction) { self.touchAction(); } if ([self.delegate respondsToSelector:@selector(didClickRepayComment:) ]){ [self.delegate didClickRepayComment:self.currentModel]; } } - (void)zanHander:(UIButton *)button { button.selected = !button.selected; if (self.touchZan) { self.touchZan(); } if ([self.delegate respondsToSelector:@selector(didClickZanComment:withStatus:)]) { [self.delegate didClickZanComment:self.currentModel withStatus:button.selected]; } } #pragma mark - setter/getter - (UIImageView *)portraitImgVi { if (!_portraitImgVi) { _portraitImgVi = [[UIImageView alloc] init]; _portraitImgVi.layer.cornerRadius = 5.f; _portraitImgVi.layer.masksToBounds = YES; } return _portraitImgVi; } - (UILabel *)nameLabel { if (!_nameLabel) { _nameLabel = [UILabel new]; _nameLabel.font = [UIFont systemFontOfSize:15.f]; _nameLabel.textColor = k6; _nameLabel.text = @"占位"; } return _nameLabel; } - (UILabel *)floorLabel { if (!_floorLabel) { _floorLabel = [UILabel new]; _floorLabel.font = [UIFont systemFontOfSize:14.f]; _floorLabel.textColor = k9; _floorLabel.text = @"占位"; } return _floorLabel; } - (UILabel *)timeLabel { if (!_timeLabel) { _timeLabel = [UILabel new]; _timeLabel.font = [UIFont systemFontOfSize:14.f]; _timeLabel.textColor = UIColorHex(bbbbbb); _timeLabel.text = @"占位"; } return _timeLabel; } - (UILabel *)contentLabel { if (!_contentLabel) { _contentLabel = [UILabel new]; _contentLabel.numberOfLines = 0; _contentLabel.font = [UIFont systemFontOfSize:15.f]; _contentLabel.textColor = k6; _contentLabel.text = @"占位"; } return _contentLabel; } - (TDButton *)replyButton { if (!_replyButton) { _replyButton = [[TDButton alloc] init]; // _replyButton.backgroundColor = UIColorHex(F6F6F6); // _replyButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; // _replyButton.contentEdgeInsets = UIEdgeInsetsMake(0, 15, 0, -15); [_replyButton addTarget:self action:@selector(replyAction:) forControlEvents:UIControlEventTouchUpInside]; } return _replyButton; } - (TDButton *)zanBtn{ if (!_zanBtn) { _zanBtn = [[TDButton alloc] init]; [_zanBtn setImage:IMG(@"zan_yes") forState:UIControlStateSelected]; [_zanBtn setImage:IMG(@"zan_no") forState:UIControlStateNormal]; [_zanBtn setCurrentButtonHotSize:CGSizeZero]; [_zanBtn addTarget:self action:@selector(zanHander:) forControlEvents:UIControlEventTouchUpInside]; } return _zanBtn; } - (UILabel *)repaylab{ if (!_repaylab) { _repaylab = [UILabel new]; } return _repaylab; } - (CommentModel *)currentModel{ if (!_currentModel) { _currentModel = [[CommentModel alloc] init]; } return _currentModel; } - (TDTableView *)myTableView{ if (!_myTableView) { _myTableView = [[TDTableView alloc]init]; _myTableView.delegate = self; _myTableView.dataSource = self; } return _myTableView; } - (NSMutableArray *)repayArrData{ if (!_repayArrData) { _repayArrData = [NSMutableArray array]; } return _repayArrData; } @end