EaseUserCell.m 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /************************************************************
  2. * * Hyphenate CONFIDENTIAL
  3. * __________________
  4. * Copyright (C) 2016 Hyphenate Inc. All rights reserved.
  5. *
  6. * NOTICE: All information contained herein is, and remains
  7. * the property of Hyphenate Inc.
  8. * Dissemination of this information or reproduction of this material
  9. * is strictly forbidden unless prior written permission is obtained
  10. * from Hyphenate Inc.
  11. */
  12. #import "EaseUserCell.h"
  13. #import "EaseImageView.h"
  14. #import "UIImageView+EMWebCache.h"
  15. CGFloat const EaseUserCellPadding = 10;
  16. @interface EaseUserCell()
  17. @property (nonatomic) NSLayoutConstraint *titleWithAvatarLeftConstraint;
  18. @property (nonatomic) NSLayoutConstraint *titleWithoutAvatarLeftConstraint;
  19. @end
  20. @implementation EaseUserCell
  21. + (void)initialize
  22. {
  23. // UIAppearance Proxy Defaults
  24. /** @brief 默认配置 */
  25. EaseUserCell *cell = [self appearance];
  26. cell.titleLabelColor = [UIColor blackColor];
  27. cell.titleLabelFont = [UIFont systemFontOfSize:18];
  28. }
  29. - (instancetype)initWithStyle:(UITableViewCellStyle)style
  30. reuseIdentifier:(NSString *)reuseIdentifier
  31. {
  32. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  33. if (self) {
  34. self.accessibilityIdentifier = @"table_cell";
  35. [self _setupSubview];
  36. UILongPressGestureRecognizer *headerLongPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(headerLongPress:)];
  37. [self addGestureRecognizer:headerLongPress];
  38. }
  39. return self;
  40. }
  41. #pragma mark - private layout subviews
  42. /*!
  43. @method
  44. @brief 加载视图
  45. @discussion
  46. @return
  47. */
  48. - (void)_setupSubview
  49. {
  50. _avatarView = [[EaseImageView alloc] init];
  51. _avatarView.translatesAutoresizingMaskIntoConstraints = NO;
  52. [self.contentView addSubview:_avatarView];
  53. _titleLabel = [[UILabel alloc] init];
  54. _titleLabel.accessibilityIdentifier = @"title";
  55. _titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
  56. _titleLabel.numberOfLines = 2;
  57. _titleLabel.backgroundColor = [UIColor clearColor];
  58. _titleLabel.font = _titleLabelFont;
  59. _titleLabel.textColor = _titleLabelColor;
  60. [self.contentView addSubview:_titleLabel];
  61. [self _setupAvatarViewConstraints];
  62. [self _setupTitleLabelConstraints];
  63. }
  64. #pragma mark - Setup Constraints
  65. /*!
  66. @method
  67. @brief 设置avatarView的约束
  68. @discussion
  69. @return
  70. */
  71. - (void)_setupAvatarViewConstraints
  72. {
  73. [self addConstraint:[NSLayoutConstraint constraintWithItem:self.avatarView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:EaseUserCellPadding]];
  74. [self addConstraint:[NSLayoutConstraint constraintWithItem:self.avatarView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-EaseUserCellPadding]];
  75. [self addConstraint:[NSLayoutConstraint constraintWithItem:self.avatarView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:EaseUserCellPadding]];
  76. [self addConstraint:[NSLayoutConstraint constraintWithItem:self.avatarView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
  77. [self addConstraint:[NSLayoutConstraint constraintWithItem:self.avatarView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.avatarView attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0]];
  78. }
  79. /*!
  80. @method
  81. @brief 设置titleLabel的约束
  82. @discussion
  83. @return
  84. */
  85. - (void)_setupTitleLabelConstraints
  86. {
  87. [self addConstraint:[NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:EaseUserCellPadding]];
  88. [self addConstraint:[NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-EaseUserCellPadding]];
  89. [self addConstraint:[NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
  90. [self addConstraint:[NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-EaseUserCellPadding]];
  91. self.titleWithAvatarLeftConstraint = [NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.avatarView attribute:NSLayoutAttributeRight multiplier:1.0 constant:EaseUserCellPadding];
  92. self.titleWithoutAvatarLeftConstraint = [NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:EaseUserCellPadding];
  93. [self addConstraint:self.titleWithAvatarLeftConstraint];
  94. }
  95. #pragma mark - setter
  96. - (void)setShowAvatar:(BOOL)showAvatar
  97. {
  98. if (_showAvatar != showAvatar) {
  99. _showAvatar = showAvatar;
  100. self.avatarView.hidden = !showAvatar;
  101. if (_showAvatar) {
  102. [self removeConstraint:self.titleWithoutAvatarLeftConstraint];
  103. [self addConstraint:self.titleWithAvatarLeftConstraint];
  104. }
  105. else{
  106. [self removeConstraint:self.titleWithAvatarLeftConstraint];
  107. [self addConstraint:self.titleWithoutAvatarLeftConstraint];
  108. }
  109. }
  110. }
  111. - (void)setModel:(id<IUserModel>)model
  112. {
  113. _model = model;
  114. if ([_model.nickname length] > 0) {
  115. self.titleLabel.text = _model.nickname;
  116. }
  117. else{
  118. self.titleLabel.text = _model.buddy;
  119. }
  120. if ([_model.avatarURLPath length] > 0){
  121. [self.avatarView.imageView sd_setImageWithURL:[NSURL URLWithString:_model.avatarURLPath] placeholderImage:_model.avatarImage];
  122. } else {
  123. if (_model.avatarImage) {
  124. self.avatarView.image = _model.avatarImage;
  125. }
  126. }
  127. }
  128. - (void)setTitleLabelFont:(UIFont *)titleLabelFont
  129. {
  130. _titleLabelFont = titleLabelFont;
  131. _titleLabel.font = _titleLabelFont;
  132. }
  133. - (void)setTitleLabelColor:(UIColor *)titleLabelColor
  134. {
  135. _titleLabelColor = titleLabelColor;
  136. _titleLabel.textColor = _titleLabelColor;
  137. }
  138. #pragma mark - class method
  139. /*!
  140. @method
  141. @brief 获取cell的重用标识
  142. @discussion
  143. @param model 消息model
  144. @return 返回cell的重用标识
  145. */
  146. + (NSString *)cellIdentifierWithModel:(id)model
  147. {
  148. return @"EaseUserCell";
  149. }
  150. /*!
  151. @method
  152. @brief 获取cell的高度
  153. @discussion
  154. @param model 消息model
  155. @return 返回cell的高度
  156. */
  157. + (CGFloat)cellHeightWithModel:(id)model
  158. {
  159. return EaseUserCellMinHeight;
  160. }
  161. #pragma mark - action
  162. /*!
  163. @method
  164. @brief 头像长按事件
  165. @discussion
  166. @param longPress 长按手势
  167. @return
  168. */
  169. - (void)headerLongPress:(UILongPressGestureRecognizer *)longPress
  170. {
  171. if (longPress.state == UIGestureRecognizerStateBegan) {
  172. if(_delegate && _indexPath && [_delegate respondsToSelector:@selector(cellLongPressAtIndexPath:)])
  173. {
  174. [_delegate cellLongPressAtIndexPath:self.indexPath];
  175. }
  176. }
  177. }
  178. - (void)setSelected:(BOOL)selected animated:(BOOL)animated
  179. {
  180. [super setSelected:selected animated:animated];
  181. if (_avatarView.badge) {
  182. _avatarView.badgeBackgroudColor = [UIColor redColor];
  183. }
  184. }
  185. -(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
  186. [super setHighlighted:highlighted animated:animated];
  187. if (_avatarView.badge) {
  188. _avatarView.badgeBackgroudColor = [UIColor redColor];
  189. }
  190. }
  191. @end