XYPersinalContentTableViewCell.m 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //
  2. // XYPersinalContentTableViewCell.m
  3. // Starbuds
  4. //
  5. // Created by 翟玉磊 on 2019/12/21.
  6. // Copyright © 2019 翟玉磊. All rights reserved.
  7. //
  8. #import "XYPersinalContentTableViewCell.h"
  9. #import "XYPersonalCenterListModel.h"
  10. @interface XYPersinalContentTableViewCell ()
  11. /// cell的白底背景
  12. @property (nonatomic, readwrite, strong) UIView *infoView;
  13. /// icon
  14. @property (nonatomic, readwrite, strong) UIImageView *titleImageView;
  15. /// 标题
  16. @property (nonatomic, readwrite, strong) UILabel *titleLabel;
  17. /// 更多图片
  18. @property (nonatomic, readwrite, strong) UIImageView *moreImageView;
  19. /// 附加内容 显示在靠右变
  20. @property (nonatomic, readwrite, strong) UILabel *contentLabel;
  21. /// 附加内容的右边和父view的约束
  22. @property (nonatomic, readwrite, strong) MASConstraint *contentRightConstraint;
  23. /// 附加内容 显示在contentLabel左边
  24. @property (nonatomic, strong) UIImageView *contentImageView;
  25. @end
  26. @implementation XYPersinalContentTableViewCell
  27. #pragma mark - Public Method
  28. + (instancetype)cellWithTableView:(UITableView *)tableView {
  29. static NSString * const cellId = @"XYPersinalContentTableViewCell";
  30. XYPersinalContentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
  31. if (cell == nil) {
  32. cell = [[XYPersinalContentTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
  33. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  34. }
  35. return cell;
  36. }
  37. - (void)configureModel:(XYPersonalCenterListModel *)model {
  38. self.titleImageView.image = ImageNamed(model.iconName);
  39. self.titleLabel.text = model.title;
  40. self.moreImageView.hidden = !model.isShowMoreImage;
  41. self.contentLabel.text = model.content;
  42. [self.contentImageView sd_setImageWithURL:UrlForString(model.imageContent) placeholderImage:nil];
  43. self.contentRightConstraint.equalTo(@(model.isShowMoreImage?(-(15.0f + 6.0f + 4.0f)):-15.0f));
  44. if ([model.title isEqualToString:@"我的芝麻"]) {
  45. self.titleLabel.textColor = Color_TextRed;
  46. }else {
  47. self.titleLabel.textColor = Color_TextFont;
  48. }
  49. }
  50. - (void)setIndexPath:(NSIndexPath *)indexPath rowsInSection:(NSInteger)rows {
  51. if (indexPath.row == 0 && rows == 1) {
  52. // 说明只有一个cell
  53. [self.infoView addViewBorderWithCorners:UIRectCornerAllCorners color:Color_Clear redian:8];
  54. }else if (indexPath.row == 0 && rows != 1) {
  55. // 说明是第一个cell且section下不止一个cell
  56. [self.infoView addViewBorderWithCorners:UIRectCornerTopLeft|UIRectCornerTopRight color:Color_Clear redian:8];
  57. }else if (indexPath.row == rows && rows != 1) {
  58. // 说明是最后一个cell且section下不止一个cell
  59. [self.infoView addViewBorderWithCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight color:Color_Clear redian:8];
  60. }else {
  61. // 中间的cell重置边框
  62. [self.infoView addViewBorder:Color_Clear redian:0];
  63. }
  64. }
  65. #pragma mark - 初始化
  66. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
  67. if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
  68. // 初始化
  69. [self _setup];
  70. // 创建自控制器
  71. [self _setupSubViews];
  72. // 布局子控件
  73. [self _makeSubViewsConstraints];
  74. }
  75. return self;
  76. }
  77. #pragma mark - 事件处理Or辅助方法
  78. #pragma mark - Private Method
  79. - (void)_setup{
  80. self.backgroundColor = Color_Clear;
  81. }
  82. #pragma mark - 创建子控件
  83. - (void)_setupSubViews{
  84. [self.contentView addSubview:self.infoView];
  85. [self.infoView addSubview:self.titleImageView];
  86. [self.infoView addSubview:self.titleLabel];
  87. [self.infoView addSubview:self.moreImageView];
  88. [self.infoView addSubview:self.contentLabel];
  89. [self.infoView addSubview:self.contentImageView];
  90. }
  91. #pragma mark - 布局子控件
  92. - (void)_makeSubViewsConstraints{
  93. [self.infoView mas_makeConstraints:^(MASConstraintMaker *make) {
  94. make.left.equalTo(self.contentView).offset(0.0f);
  95. make.right.equalTo(self.contentView).offset(0.0f);
  96. make.top.bottom.equalTo(self.contentView);
  97. }];
  98. [self.titleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  99. make.left.equalTo(self.infoView).offset(16.0f);
  100. make.centerY.equalTo(self.infoView);
  101. make.width.height.equalTo(@18.0f);
  102. }];
  103. [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  104. make.left.equalTo(self.titleImageView.mas_right).offset(12.0f);
  105. make.centerY.equalTo(self.infoView);
  106. make.right.equalTo(self.contentLabel.mas_left).offset(-10.0f);
  107. }];
  108. [self.moreImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  109. make.right.equalTo(self.infoView).offset(-15.0f);
  110. make.centerY.equalTo(self.infoView);
  111. make.width.equalTo(@6.0f);
  112. make.height.equalTo(@10.0f);
  113. }];
  114. [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  115. self.contentRightConstraint = make.right.equalTo(self.infoView).offset(-(15.0f + 12.0f + 10.0f));
  116. make.centerY.equalTo(self.infoView);
  117. }];
  118. [self.contentImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  119. make.right.equalTo(self.contentLabel.mas_left).offset(-4.0f);
  120. make.centerY.equalTo(self.infoView);
  121. make.width.height.equalTo(@20.0f);
  122. }];
  123. }
  124. - (UIView *)infoView {
  125. if (!_infoView) {
  126. _infoView = [UIView new];
  127. // 因为要设置边框索引需要先有frame
  128. _infoView.frame = self.bounds;
  129. _infoView.backgroundColor = Color_White;
  130. }
  131. return _infoView;;
  132. }
  133. - (UIImageView *)titleImageView {
  134. if (!_titleImageView) {
  135. _titleImageView = [UIImageView new];
  136. }
  137. return _titleImageView;
  138. }
  139. - (UILabel *)titleLabel {
  140. if (!_titleLabel) {
  141. _titleLabel = [UILabel createLabelTextColor:Color_TextFont fount:Font(15)];
  142. }
  143. return _titleLabel;
  144. }
  145. - (UIImageView *)moreImageView {
  146. if (!_moreImageView) {
  147. _moreImageView = [UIImageView new];
  148. _moreImageView.image = arrowMore();
  149. }
  150. return _moreImageView;
  151. }
  152. - (UILabel *)contentLabel {
  153. if (!_contentLabel) {
  154. _contentLabel = [UILabel createLabelTextColor:Color_TextGray fount:Font(12)];
  155. _contentLabel.textAlignment = NSTextAlignmentRight;
  156. }
  157. return _contentLabel;
  158. }
  159. - (UIImageView *)contentImageView {
  160. if (_contentImageView == nil) {
  161. _contentImageView = [UIImageView new];
  162. }
  163. return _contentImageView;
  164. }
  165. - (void)awakeFromNib {
  166. [super awakeFromNib];
  167. // Initialization code
  168. }
  169. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  170. [super setSelected:selected animated:animated];
  171. // Configure the view for the selected state
  172. }
  173. @end