YMSheetPopupView.m 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. //
  2. // YMSheetPopupView.m
  3. // MSYOUPAI
  4. //
  5. // Created by YoMi on 2024/3/4.
  6. // Copyright © 2024 MS. All rights reserved.
  7. //
  8. #import "YMSheetPopupView.h"
  9. @interface YMSheetPopupView ()<UITableViewDelegate,UITableViewDataSource>
  10. /** 内容列表视图*/
  11. @property (nonatomic, strong) UITableView *contentTableView;
  12. /** 关闭按钮*/
  13. @property (nonatomic, strong) UIButton *closeBtn;
  14. /** 项目列表 */
  15. @property (nonatomic, strong) NSArray *itemList;
  16. @end
  17. @implementation YMSheetPopupView
  18. - (void)ym_setupViews{
  19. self.backgroundColor = UIColor.whiteColor;
  20. [self addSubview:self.contentTableView];
  21. [self addSubview:self.closeBtn];
  22. [self setNeedsUpdateConstraints];
  23. [self updateConstraintsIfNeeded];
  24. }
  25. - (void)updateConstraints{
  26. [self.contentTableView mas_makeConstraints:^(MASConstraintMaker *make) {
  27. make.top.equalTo(self);
  28. make.left.equalTo(self);
  29. make.right.equalTo(self);
  30. }];
  31. [self.closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  32. make.top.equalTo(self.contentTableView.mas_bottom).priorityLow();
  33. make.left.equalTo(self);
  34. make.right.equalTo(self);
  35. make.bottom.equalTo(self).offset(Is_iPhoneX ? adapt(-22) : adapt(-12)).priorityHigh();
  36. make.height.mas_equalTo(adapt(45));
  37. }];
  38. [super updateConstraints];
  39. }
  40. - (void)configutationWithItemList:(NSArray*)itemList{
  41. self.itemList = itemList;
  42. CGFloat closeBtnHeight = [self.closeBtn systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
  43. [self.contentTableView reloadData];
  44. [self.contentTableView layoutIfNeeded];
  45. //刷新高度
  46. CGFloat tableViewHeight = self.contentTableView.contentSize.height;
  47. [self.contentTableView mas_updateConstraints:^(MASConstraintMaker *make) {
  48. make.height.mas_equalTo(tableViewHeight);
  49. }];
  50. self.frame = CGRectMake(0, 0, kFrameWidth, tableViewHeight + closeBtnHeight + (Is_iPhoneX ? adapt(22) : adapt(12)));
  51. }
  52. #pragma mark - UITableViewDataSource
  53. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  54. return 1;
  55. }
  56. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  57. return self.itemList.count;
  58. }
  59. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  60. YMSheetCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([YMSheetCell class])];
  61. if (!cell) {
  62. cell = [[YMSheetCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:NSStringFromClass([YMSheetCell class])];
  63. }
  64. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  65. [cell ym_bindViewModel:self.itemList[indexPath.item]];
  66. return cell;
  67. }
  68. #pragma mark - UITableViewDelegate
  69. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  70. return adapt(45);
  71. }
  72. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
  73. return nil;
  74. }
  75. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  76. return CGFLOAT_MIN;
  77. }
  78. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
  79. return nil;
  80. }
  81. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  82. return CGFLOAT_MIN;
  83. }
  84. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  85. if (self.selectIndexBlock) {
  86. self.selectIndexBlock(indexPath.item);
  87. }
  88. }
  89. - (UITableView *)contentTableView{
  90. if (!_contentTableView) {
  91. _contentTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
  92. _contentTableView.delegate = self;
  93. _contentTableView.dataSource = self;
  94. _contentTableView.scrollEnabled = NO;
  95. _contentTableView.estimatedRowHeight = 0;
  96. _contentTableView.estimatedSectionHeaderHeight = 0;
  97. _contentTableView.estimatedSectionFooterHeight = 0;
  98. _contentTableView.showsVerticalScrollIndicator = NO;
  99. _contentTableView.showsHorizontalScrollIndicator = NO;
  100. _contentTableView.separatorColor = UIColor.clearColor;
  101. _contentTableView.backgroundColor = UIColor.whiteColor;
  102. [_contentTableView registerClass:[YMSheetCell class] forCellReuseIdentifier:NSStringFromClass([YMSheetCell class])];
  103. }
  104. return _contentTableView;
  105. }
  106. - (UIButton *)closeBtn {
  107. if(!_closeBtn){
  108. _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  109. _closeBtn.titleLabel.font = LCFont(15);
  110. [_closeBtn setTitleColor:HexColorFromRGB(0x9c9c9c) forState:UIControlStateNormal];
  111. [_closeBtn setTitle:@"取消" forState:UIControlStateNormal];
  112. WS(weakSelf)
  113. [[[_closeBtn rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id x) {
  114. if (weakSelf.closeBlock) {
  115. weakSelf.closeBlock();
  116. }
  117. }];
  118. }
  119. return _closeBtn;
  120. }
  121. @end
  122. @interface YMSheetCell ()
  123. /** 提示标题*/
  124. @property (nonatomic, strong) UILabel *tipsTitleLb;
  125. /** 分割线*/
  126. @property (nonatomic, strong) UIView *dividedLine;
  127. @end
  128. @implementation YMSheetCell
  129. - (void)awakeFromNib {
  130. [super awakeFromNib];
  131. // Initialization code
  132. }
  133. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  134. [super setSelected:selected animated:animated];
  135. // Configure the view for the selected state
  136. }
  137. - (void)ym_setupViews{
  138. [self.contentView addSubview:self.tipsTitleLb];
  139. [self.contentView addSubview:self.dividedLine];
  140. [self setNeedsUpdateConstraints];
  141. [self updateConstraintsIfNeeded];
  142. }
  143. - (void)updateConstraints{
  144. [self.tipsTitleLb mas_makeConstraints:^(MASConstraintMaker *make) {
  145. make.centerY.equalTo(self.contentView.mas_centerY);
  146. make.left.equalTo(self.contentView).offset(adapt(10));
  147. make.right.equalTo(self.contentView).offset(adapt(-10));
  148. }];
  149. [self.dividedLine mas_makeConstraints:^(MASConstraintMaker *make) {
  150. make.bottom.equalTo(self.contentView.mas_bottom);
  151. make.left.equalTo(self.contentView).offset(adapt(10));
  152. make.right.equalTo(self.contentView).offset(adapt(-10));
  153. make.height.mas_equalTo(adapt(0.8));
  154. }];
  155. [super updateConstraints];
  156. }
  157. - (void)ym_bindViewModel:(id)viewModel{
  158. if ([viewModel isKindOfClass:[NSString class]]) {
  159. self.tipsTitleLb.text = viewModel;
  160. }
  161. }
  162. - (UILabel *)tipsTitleLb{
  163. if (!_tipsTitleLb) {
  164. _tipsTitleLb = [[UILabel alloc]init];
  165. _tipsTitleLb.font = LCFont(15);
  166. _tipsTitleLb.textColor = HexColorFromRGB(0x333333);
  167. _tipsTitleLb.textAlignment = NSTextAlignmentCenter;
  168. _tipsTitleLb.text = @"******";
  169. }
  170. return _tipsTitleLb;
  171. }
  172. - (UIView *)dividedLine{
  173. if (!_dividedLine) {
  174. _dividedLine = [[UIView alloc]init];
  175. _dividedLine.backgroundColor = HexColorFromRGB(0xF6F6F6);
  176. }
  177. return _dividedLine;
  178. }
  179. @end