// // YMSheetPopupView.m // MSYOUPAI // // Created by YoMi on 2024/3/4. // Copyright © 2024 MS. All rights reserved. // #import "YMSheetPopupView.h" @interface YMSheetPopupView () /** 内容列表视图*/ @property (nonatomic, strong) UITableView *contentTableView; /** 关闭按钮*/ @property (nonatomic, strong) UIButton *closeBtn; /** 项目列表 */ @property (nonatomic, strong) NSArray *itemList; @end @implementation YMSheetPopupView - (void)ym_setupViews{ self.backgroundColor = UIColor.whiteColor; [self addSubview:self.contentTableView]; [self addSubview:self.closeBtn]; [self setNeedsUpdateConstraints]; [self updateConstraintsIfNeeded]; } - (void)updateConstraints{ [self.contentTableView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self); make.left.equalTo(self); make.right.equalTo(self); }]; [self.closeBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.contentTableView.mas_bottom).priorityLow(); make.left.equalTo(self); make.right.equalTo(self); make.bottom.equalTo(self).offset(Is_iPhoneX ? adapt(-22) : adapt(-12)).priorityHigh(); make.height.mas_equalTo(adapt(45)); }]; [super updateConstraints]; } - (void)configutationWithItemList:(NSArray*)itemList{ self.itemList = itemList; CGFloat closeBtnHeight = [self.closeBtn systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height; [self.contentTableView reloadData]; [self.contentTableView layoutIfNeeded]; //刷新高度 CGFloat tableViewHeight = self.contentTableView.contentSize.height; [self.contentTableView mas_updateConstraints:^(MASConstraintMaker *make) { make.height.mas_equalTo(tableViewHeight); }]; self.frame = CGRectMake(0, 0, kFrameWidth, tableViewHeight + closeBtnHeight + (Is_iPhoneX ? adapt(22) : adapt(12))); } #pragma mark - UITableViewDataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.itemList.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ YMSheetCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([YMSheetCell class])]; if (!cell) { cell = [[YMSheetCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:NSStringFromClass([YMSheetCell class])]; } cell.selectionStyle = UITableViewCellSelectionStyleNone; [cell ym_bindViewModel:self.itemList[indexPath.item]]; return cell; } #pragma mark - UITableViewDelegate - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return adapt(45); } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ return nil; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return CGFLOAT_MIN; } - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { return nil; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return CGFLOAT_MIN; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ if (self.selectIndexBlock) { self.selectIndexBlock(indexPath.item); } } - (UITableView *)contentTableView{ if (!_contentTableView) { _contentTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain]; _contentTableView.delegate = self; _contentTableView.dataSource = self; _contentTableView.scrollEnabled = NO; _contentTableView.estimatedRowHeight = 0; _contentTableView.estimatedSectionHeaderHeight = 0; _contentTableView.estimatedSectionFooterHeight = 0; _contentTableView.showsVerticalScrollIndicator = NO; _contentTableView.showsHorizontalScrollIndicator = NO; _contentTableView.separatorColor = UIColor.clearColor; _contentTableView.backgroundColor = UIColor.whiteColor; [_contentTableView registerClass:[YMSheetCell class] forCellReuseIdentifier:NSStringFromClass([YMSheetCell class])]; } return _contentTableView; } - (UIButton *)closeBtn { if(!_closeBtn){ _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _closeBtn.titleLabel.font = LCFont(15); [_closeBtn setTitleColor:HexColorFromRGB(0x9c9c9c) forState:UIControlStateNormal]; [_closeBtn setTitle:@"取消" forState:UIControlStateNormal]; WS(weakSelf) [[[_closeBtn rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id x) { if (weakSelf.closeBlock) { weakSelf.closeBlock(); } }]; } return _closeBtn; } @end @interface YMSheetCell () /** 提示标题*/ @property (nonatomic, strong) UILabel *tipsTitleLb; /** 分割线*/ @property (nonatomic, strong) UIView *dividedLine; @end @implementation YMSheetCell - (void)awakeFromNib { [super awakeFromNib]; // Initialization code } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } - (void)ym_setupViews{ [self.contentView addSubview:self.tipsTitleLb]; [self.contentView addSubview:self.dividedLine]; [self setNeedsUpdateConstraints]; [self updateConstraintsIfNeeded]; } - (void)updateConstraints{ [self.tipsTitleLb mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(self.contentView.mas_centerY); make.left.equalTo(self.contentView).offset(adapt(10)); make.right.equalTo(self.contentView).offset(adapt(-10)); }]; [self.dividedLine mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.equalTo(self.contentView.mas_bottom); make.left.equalTo(self.contentView).offset(adapt(10)); make.right.equalTo(self.contentView).offset(adapt(-10)); make.height.mas_equalTo(adapt(0.8)); }]; [super updateConstraints]; } - (void)ym_bindViewModel:(id)viewModel{ if ([viewModel isKindOfClass:[NSString class]]) { self.tipsTitleLb.text = viewModel; } } - (UILabel *)tipsTitleLb{ if (!_tipsTitleLb) { _tipsTitleLb = [[UILabel alloc]init]; _tipsTitleLb.font = LCFont(15); _tipsTitleLb.textColor = HexColorFromRGB(0x333333); _tipsTitleLb.textAlignment = NSTextAlignmentCenter; _tipsTitleLb.text = @"******"; } return _tipsTitleLb; } - (UIView *)dividedLine{ if (!_dividedLine) { _dividedLine = [[UIView alloc]init]; _dividedLine.backgroundColor = HexColorFromRGB(0xF6F6F6); } return _dividedLine; } @end