// // MHTopicCell.m // MHDevelopExample // // Created by CoderMikeHe on 17/2/9. // Copyright © 2017年 CoderMikeHe. All rights reserved. // #import "MHTopicCell.h" #import "MHTopicFrame.h" #import "MHTopicCommentCell.h" #import "MHDivider.h" @interface MHTopicCell()< UITableViewDelegate , UITableViewDataSource,MHTopicCommentCellDelegate > /** 头像 */ @property (nonatomic , strong) UIImageView *avatarView; /** 昵称 */ @property (nonatomic , weak) YYLabel *nicknameLable; /** ContentView */ @property (nonatomic , weak) UIView *contentBaseView; /** 分割线 */ @property (nonatomic , weak) MHDivider *divider; @end @implementation MHTopicCell - (void)awakeFromNib { [super awakeFromNib]; } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; } + (instancetype)cellWithTableView:(UITableView *)tableView { static NSString *ID = @"TopicCell"; MHTopicCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (!cell) { cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; cell.contentView.backgroundColor = UIColorHex(#FAFAFA); cell.selectionStyle = UITableViewCellSelectionStyleNone; } return cell; } - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // 初始化 [self _setup]; // 创建自控制器 [self _setupSubViews]; // 布局子控件 [self _makeSubViewsConstraints]; } return self; } #pragma mark - 公共方法 #pragma mark - Setter - (void)setTopicFrame:(MHTopicFrame *)topicFrame { _topicFrame = topicFrame; MHTopic *topic = topicFrame.topic; self.avatarView.layer.cornerRadius = 4.f; self.avatarView.layer.masksToBounds = YES; // 头像 self.avatarView.frame = topicFrame.avatarFrame; [self.avatarView sd_setImageWithURL:[NSURL URLWithString:topic.AvatarUrl] placeholderImage:kUserDefaultHeadImage]; // 昵称 self.nicknameLable.frame = topicFrame.nicknameFrame; // self.nicknameLable.text = user.nickname; self.nicknameLable.text = topic.Name; // 点赞 self.thumbBtn.frame = topicFrame.thumbFrame; self.thumbBtn.selected = topic.IsLaud; // 时间 self.createTimeLabel.frame = topicFrame.createTimeFrame; // 内容 self.contentLabel.frame = topicFrame.textFrame; self.contentLabel.attributedText = topic.attributedText; // 刷新评论tableView self.tableView.frame = topicFrame.tableViewFrame; [self.tableView reloadData]; } #pragma mark - 私有方法 #pragma mark - 初始化 - (void)_setup { // 设置颜色 self.contentView.backgroundColor = [UIColor whiteColor]; } #pragma mark - 创建自控制器 - (void)_setupSubViews { // 头像 self.avatarView = [UIImageView new]; [self.contentView addSubview:self.avatarView]; self.avatarView.userInteractionEnabled = YES; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(_avatarOrNicknameDidClicked)]; [self.avatarView addGestureRecognizer:tap]; UILongPressGestureRecognizer *longPre = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressSelf:)]; [self.contentView addGestureRecognizer:longPre]; // 昵称 YYLabel *nicknameLable = [[YYLabel alloc] init]; nicknameLable.text = @""; nicknameLable.font = [UIFont systemFontOfSize:17]; nicknameLable.textAlignment = NSTextAlignmentLeft; nicknameLable.textColor = UIColorHex(#0A0A0A); [self.contentView addSubview:nicknameLable]; self.nicknameLable = nicknameLable; __weak typeof(self) weakSelf = self; nicknameLable.textTapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) { // [weakSelf _avatarOrNicknameDidClicked]; }; // 点赞按钮 self.thumbBtn = [[TDButton alloc] init]; [self.thumbBtn setImage:IMG(@"zan_no") forState:UIControlStateNormal]; [self.thumbBtn setImage:IMG(@"zan_yes") forState:UIControlStateSelected]; [self.thumbBtn setCurrentButtonHotSize:CGSizeMake(18, 19)]; [self.contentView addSubview:self.thumbBtn]; // 时间 YYLabel *createTimeLabel = [[YYLabel alloc] init]; createTimeLabel.text = @""; createTimeLabel.font = [UIFont systemFontOfSize:14]; createTimeLabel.textAlignment = NSTextAlignmentLeft; createTimeLabel.textColor = UIColorHex(#BBBBBB); [self.contentView addSubview:createTimeLabel]; self.createTimeLabel = createTimeLabel; // createTimeLabel.textTapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) { // [weakSelf _contentTextDidClicked]; // }; // 文本 YYLabel *contentLabel = [[YYLabel alloc] init]; UIEdgeInsets textContainerInset = contentLabel.textContainerInset; textContainerInset.top = 12; textContainerInset.bottom = 12; contentLabel.textContainerInset = textContainerInset; contentLabel.numberOfLines = 0; [contentLabel setFont:[UIFont systemFontOfSize:17]]; [self.contentView addSubview:contentLabel]; self.contentLabel = contentLabel; contentLabel.textTapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) { if (weakSelf.replyContentBlock) { weakSelf.replyContentBlock(); } }; // UITableView UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain]; tableView.separatorStyle = UITableViewCellSeparatorStyleNone; tableView.delegate = self; tableView.dataSource = self; tableView.bounces = NO; tableView.scrollEnabled = NO; tableView.showsVerticalScrollIndicator = NO; tableView.showsHorizontalScrollIndicator = NO; tableView.backgroundColor = [UIColor whiteColor]; [self.contentView addSubview:tableView]; self.tableView = tableView; // 分割线 MHDivider *divider = [MHDivider divider]; self.divider = divider; [self.contentView addSubview:divider]; } #pragma mark - 布局子控件 - (void)_makeSubViewsConstraints { } #pragma mark - override #pragma mark - 布局子控件 - (void)layoutSubviews { [super layoutSubviews]; // 布局子控件 self.divider.frame = CGRectMake(21, self.frame.size.height - MHGlobalBottomLineHeight, self.frame.size.width - 42, MHGlobalBottomLineHeight); } #pragma mark - 事件处理 - (void)_moreBtnDidClicked:(UIButton *)sender { if (self.delegate && [self.delegate respondsToSelector:@selector(topicCellForClickedMoreAction:)]) { [self.delegate topicCellForClickedMoreAction:self]; } } - (void)_avatarOrNicknameDidClicked { if (self.delegate && [self.delegate respondsToSelector:@selector(topicCellDidClickedUser:)]) { [self.delegate topicCellDidClickedUser:self]; } } //- (void)_contentTextDidClicked //{ // if (self.delegate && [self.delegate respondsToSelector:@selector(topicCellDidClickedTopicContent:)]) { // [self.delegate topicCellDidClickedTopicContent:self]; // } //} #pragma mark - UITableViewDelegate , UITableViewDataSource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.topicFrame.commentFrames.count; } - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { MHTopicCommentCell *cell = [MHTopicCommentCell cellWithTableView:tableView]; MHCommentFrame *commentFrame = self.topicFrame.commentFrames[indexPath.row]; cell.commentFrame = commentFrame; UILongPressGestureRecognizer * longComment = [[UILongPressGestureRecognizer alloc] initWithActionBlock:^(id _Nonnull sender) { NSLog(@"%@",commentFrame); if (commentFrame.comment.UserId == [AppUserModel sharedAppUserModel].Id) { if (self.longSubBlock) { self.longSubBlock([commentFrame.comment.Id integerValue],(UILabel *)cell.contentLabel); } } }]; [cell addGestureRecognizer:longComment]; cell.userInteractionEnabled = YES; cell.delegate = self; return cell; } - (void)longPressSelf:(UILongPressGestureRecognizer *)sender { if (self.longBlock) { self.longBlock(); } } - (void)longPressComment:(UILongPressGestureRecognizer *)sender { UILongPressGestureRecognizer * press = (UILongPressGestureRecognizer *)sender; if (press.state == UIGestureRecognizerStateBegan) { if (self.longBlock) { self.longBlock(); } } } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { MHCommentFrame *commentFrame = self.topicFrame.commentFrames[indexPath.row]; return commentFrame.cellHeight; } - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; if (self.delegate && [self.delegate respondsToSelector:@selector(topicCell:didSelectRowAtIndexPath:)]) { [self.delegate topicCell:self didSelectRowAtIndexPath:indexPath]; } } #pragma mark - MHTopicCommentCellDelegate - (void) topicCommentCell:(MHTopicCommentCell *)topicCommentCell didClickedUser:(NSString *)userId { if (self.delegate && [self.delegate respondsToSelector:@selector(topicCell:didClickedUser:)]) { [self.delegate topicCell:self didClickedUser:userId]; } } @end