123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- //
- // 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
|