MHTopicCommentCell.m 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //
  2. // MHTopicCommentCell.m
  3. // MHDevelopExample
  4. //
  5. // Created by CoderMikeHe on 17/2/9.
  6. // Copyright © 2017年 CoderMikeHe. All rights reserved.
  7. //
  8. #import "MHTopicCommentCell.h"
  9. #import "MHCommentFrame.h"
  10. @interface MHTopicCommentCell ()
  11. @end
  12. @implementation MHTopicCommentCell
  13. - (void)awakeFromNib {
  14. [super awakeFromNib];
  15. }
  16. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  17. [super setSelected:selected animated:animated];
  18. }
  19. + (instancetype)cellWithTableView:(UITableView *)tableView
  20. {
  21. static NSString *ID = @"CommentCell";
  22. MHTopicCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
  23. if (!cell) {
  24. cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
  25. cell.contentView.backgroundColor = UIColorHex(#FAFAFA);
  26. // cell.selectionStyle = UITableViewCellSelectionStyleNone;
  27. }
  28. return cell;
  29. }
  30. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  31. {
  32. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  33. if (self)
  34. {
  35. self.contentView.backgroundColor = UIColorHex(#FAFAFA);
  36. // 创建自控制器
  37. [self _setupSubViews];
  38. // 布局子控件
  39. [self _makeSubViewsConstraints];
  40. }
  41. return self;
  42. }
  43. #pragma mark - 公共方法
  44. - (void)setCommentFrame:(MHCommentFrame *)commentFrame
  45. {
  46. _commentFrame = commentFrame;
  47. MHComment *comment = commentFrame.comment;
  48. // 赋值
  49. self.contentLabel.frame = commentFrame.textFrame;
  50. // 设置值
  51. self.contentLabel.attributedText = comment.attributedText;
  52. }
  53. #pragma mark - 私有方法
  54. #pragma mark - 创建自控制器
  55. - (void)_setupSubViews
  56. {
  57. // 文本
  58. YYLabel *contentLabel = [[YYLabel alloc] init];
  59. contentLabel.numberOfLines = 0;
  60. [self.contentView addSubview:contentLabel];
  61. self.contentLabel = contentLabel;
  62. __weak typeof(self) weakSelf = self;
  63. contentLabel.highlightTapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
  64. // 利用KVC获取UserInfo 其实可以在MHComment模型里面利用 通知告知控制器哪个用户被点击了
  65. YYTextHighlight *highlight = [containerView valueForKeyPath:@"_highlight"];
  66. if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(topicCommentCell:didClickedUser:)]) {
  67. [weakSelf.delegate topicCommentCell:weakSelf didClickedUser:highlight.userInfo[MHCommentUserKey]];
  68. }
  69. };
  70. }
  71. #pragma mark - 布局子控件
  72. - (void)_makeSubViewsConstraints
  73. {
  74. }
  75. @end