MHTopicCell.m 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. //
  2. // MHTopicCell.m
  3. // MHDevelopExample
  4. //
  5. // Created by CoderMikeHe on 17/2/9.
  6. // Copyright © 2017年 CoderMikeHe. All rights reserved.
  7. //
  8. #import "MHTopicCell.h"
  9. #import "MHTopicFrame.h"
  10. #import "MHTopicCommentCell.h"
  11. #import "MHDivider.h"
  12. @interface MHTopicCell()< UITableViewDelegate , UITableViewDataSource,MHTopicCommentCellDelegate >
  13. /** 头像 */
  14. @property (nonatomic , strong) UIImageView *avatarView;
  15. /** 昵称 */
  16. @property (nonatomic , weak) YYLabel *nicknameLable;
  17. /** ContentView */
  18. @property (nonatomic , weak) UIView *contentBaseView;
  19. /** 分割线 */
  20. @property (nonatomic , weak) MHDivider *divider;
  21. @end
  22. @implementation MHTopicCell
  23. - (void)awakeFromNib
  24. {
  25. [super awakeFromNib];
  26. }
  27. - (void)setSelected:(BOOL)selected animated:(BOOL)animated
  28. {
  29. [super setSelected:selected animated:animated];
  30. }
  31. + (instancetype)cellWithTableView:(UITableView *)tableView
  32. {
  33. static NSString *ID = @"TopicCell";
  34. MHTopicCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
  35. if (!cell) {
  36. cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
  37. cell.contentView.backgroundColor = UIColorHex(#FAFAFA);
  38. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  39. }
  40. return cell;
  41. }
  42. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  43. {
  44. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  45. if (self)
  46. {
  47. // 初始化
  48. [self _setup];
  49. // 创建自控制器
  50. [self _setupSubViews];
  51. // 布局子控件
  52. [self _makeSubViewsConstraints];
  53. }
  54. return self;
  55. }
  56. #pragma mark - 公共方法
  57. #pragma mark - Setter
  58. - (void)setTopicFrame:(MHTopicFrame *)topicFrame
  59. {
  60. _topicFrame = topicFrame;
  61. MHTopic *topic = topicFrame.topic;
  62. self.avatarView.layer.cornerRadius = 4.f;
  63. self.avatarView.layer.masksToBounds = YES;
  64. // 头像
  65. self.avatarView.frame = topicFrame.avatarFrame;
  66. [self.avatarView sd_setImageWithURL:[NSURL URLWithString:topic.AvatarUrl] placeholderImage:kUserDefaultHeadImage];
  67. // 昵称
  68. self.nicknameLable.frame = topicFrame.nicknameFrame;
  69. // self.nicknameLable.text = user.nickname;
  70. self.nicknameLable.text = topic.Name;
  71. // 点赞
  72. self.thumbBtn.frame = topicFrame.thumbFrame;
  73. self.thumbBtn.selected = topic.IsLaud;
  74. // 时间
  75. self.createTimeLabel.frame = topicFrame.createTimeFrame;
  76. // 内容
  77. self.contentLabel.frame = topicFrame.textFrame;
  78. self.contentLabel.attributedText = topic.attributedText;
  79. // 刷新评论tableView
  80. self.tableView.frame = topicFrame.tableViewFrame;
  81. [self.tableView reloadData];
  82. }
  83. #pragma mark - 私有方法
  84. #pragma mark - 初始化
  85. - (void)_setup
  86. {
  87. // 设置颜色
  88. self.contentView.backgroundColor = [UIColor whiteColor];
  89. }
  90. #pragma mark - 创建自控制器
  91. - (void)_setupSubViews
  92. {
  93. // 头像
  94. self.avatarView = [UIImageView new];
  95. [self.contentView addSubview:self.avatarView];
  96. self.avatarView.userInteractionEnabled = YES;
  97. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(_avatarOrNicknameDidClicked)];
  98. [self.avatarView addGestureRecognizer:tap];
  99. UILongPressGestureRecognizer *longPre = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressSelf:)];
  100. [self.contentView addGestureRecognizer:longPre];
  101. // 昵称
  102. YYLabel *nicknameLable = [[YYLabel alloc] init];
  103. nicknameLable.text = @"";
  104. nicknameLable.font = [UIFont systemFontOfSize:17];
  105. nicknameLable.textAlignment = NSTextAlignmentLeft;
  106. nicknameLable.textColor = UIColorHex(#0A0A0A);
  107. [self.contentView addSubview:nicknameLable];
  108. self.nicknameLable = nicknameLable;
  109. __weak typeof(self) weakSelf = self;
  110. nicknameLable.textTapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
  111. // [weakSelf _avatarOrNicknameDidClicked];
  112. };
  113. // 点赞按钮
  114. self.thumbBtn = [[TDButton alloc] init];
  115. [self.thumbBtn setImage:IMG(@"zan_no") forState:UIControlStateNormal];
  116. [self.thumbBtn setImage:IMG(@"zan_yes") forState:UIControlStateSelected];
  117. [self.thumbBtn setCurrentButtonHotSize:CGSizeMake(18, 19)];
  118. [self.contentView addSubview:self.thumbBtn];
  119. // 时间
  120. YYLabel *createTimeLabel = [[YYLabel alloc] init];
  121. createTimeLabel.text = @"";
  122. createTimeLabel.font = [UIFont systemFontOfSize:14];
  123. createTimeLabel.textAlignment = NSTextAlignmentLeft;
  124. createTimeLabel.textColor = UIColorHex(#BBBBBB);
  125. [self.contentView addSubview:createTimeLabel];
  126. self.createTimeLabel = createTimeLabel;
  127. // createTimeLabel.textTapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
  128. // [weakSelf _contentTextDidClicked];
  129. // };
  130. // 文本
  131. YYLabel *contentLabel = [[YYLabel alloc] init];
  132. UIEdgeInsets textContainerInset = contentLabel.textContainerInset;
  133. textContainerInset.top = 12;
  134. textContainerInset.bottom = 12;
  135. contentLabel.textContainerInset = textContainerInset;
  136. contentLabel.numberOfLines = 0;
  137. [contentLabel setFont:[UIFont systemFontOfSize:17]];
  138. [self.contentView addSubview:contentLabel];
  139. self.contentLabel = contentLabel;
  140. contentLabel.textTapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
  141. if (weakSelf.replyContentBlock) {
  142. weakSelf.replyContentBlock();
  143. }
  144. };
  145. // UITableView
  146. UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
  147. tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  148. tableView.delegate = self;
  149. tableView.dataSource = self;
  150. tableView.bounces = NO;
  151. tableView.scrollEnabled = NO;
  152. tableView.showsVerticalScrollIndicator = NO;
  153. tableView.showsHorizontalScrollIndicator = NO;
  154. tableView.backgroundColor = [UIColor whiteColor];
  155. [self.contentView addSubview:tableView];
  156. self.tableView = tableView;
  157. // 分割线
  158. MHDivider *divider = [MHDivider divider];
  159. self.divider = divider;
  160. [self.contentView addSubview:divider];
  161. }
  162. #pragma mark - 布局子控件
  163. - (void)_makeSubViewsConstraints
  164. {
  165. }
  166. #pragma mark - override
  167. #pragma mark - 布局子控件
  168. - (void)layoutSubviews
  169. {
  170. [super layoutSubviews];
  171. // 布局子控件
  172. self.divider.frame = CGRectMake(21, self.frame.size.height - MHGlobalBottomLineHeight, self.frame.size.width - 42, MHGlobalBottomLineHeight);
  173. }
  174. #pragma mark - 事件处理
  175. - (void)_moreBtnDidClicked:(UIButton *)sender
  176. {
  177. if (self.delegate && [self.delegate respondsToSelector:@selector(topicCellForClickedMoreAction:)]) {
  178. [self.delegate topicCellForClickedMoreAction:self];
  179. }
  180. }
  181. - (void)_avatarOrNicknameDidClicked
  182. {
  183. if (self.delegate && [self.delegate respondsToSelector:@selector(topicCellDidClickedUser:)]) {
  184. [self.delegate topicCellDidClickedUser:self];
  185. }
  186. }
  187. //- (void)_contentTextDidClicked
  188. //{
  189. // if (self.delegate && [self.delegate respondsToSelector:@selector(topicCellDidClickedTopicContent:)]) {
  190. // [self.delegate topicCellDidClickedTopicContent:self];
  191. // }
  192. //}
  193. #pragma mark - UITableViewDelegate , UITableViewDataSource
  194. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  195. {
  196. return self.topicFrame.commentFrames.count;
  197. }
  198. - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  199. {
  200. MHTopicCommentCell *cell = [MHTopicCommentCell cellWithTableView:tableView];
  201. MHCommentFrame *commentFrame = self.topicFrame.commentFrames[indexPath.row];
  202. cell.commentFrame = commentFrame;
  203. UILongPressGestureRecognizer * longComment = [[UILongPressGestureRecognizer alloc] initWithActionBlock:^(id _Nonnull sender) {
  204. NSLog(@"%@",commentFrame);
  205. if (commentFrame.comment.UserId == [AppUserModel sharedAppUserModel].Id) {
  206. if (self.longSubBlock) {
  207. self.longSubBlock([commentFrame.comment.Id integerValue],(UILabel *)cell.contentLabel);
  208. }
  209. }
  210. }];
  211. [cell addGestureRecognizer:longComment];
  212. cell.userInteractionEnabled = YES;
  213. cell.delegate = self;
  214. return cell;
  215. }
  216. - (void)longPressSelf:(UILongPressGestureRecognizer *)sender
  217. {
  218. if (self.longBlock) {
  219. self.longBlock();
  220. }
  221. }
  222. - (void)longPressComment:(UILongPressGestureRecognizer *)sender
  223. {
  224. UILongPressGestureRecognizer * press = (UILongPressGestureRecognizer *)sender;
  225. if (press.state == UIGestureRecognizerStateBegan) {
  226. if (self.longBlock) {
  227. self.longBlock();
  228. }
  229. }
  230. }
  231. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  232. {
  233. MHCommentFrame *commentFrame = self.topicFrame.commentFrames[indexPath.row];
  234. return commentFrame.cellHeight;
  235. }
  236. - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  237. {
  238. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  239. if (self.delegate && [self.delegate respondsToSelector:@selector(topicCell:didSelectRowAtIndexPath:)]) {
  240. [self.delegate topicCell:self didSelectRowAtIndexPath:indexPath];
  241. }
  242. }
  243. #pragma mark - MHTopicCommentCellDelegate
  244. - (void) topicCommentCell:(MHTopicCommentCell *)topicCommentCell didClickedUser:(NSString *)userId
  245. {
  246. if (self.delegate && [self.delegate respondsToSelector:@selector(topicCell:didClickedUser:)]) {
  247. [self.delegate topicCell:self didClickedUser:userId];
  248. }
  249. }
  250. @end