MHTopicCell.m 10 KB

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