// // YXSelectRoomView.m // Temperature // // Created by TC on 2025/3/16. // #import "YXSelectRoomView.h" @interface YXSelectRoomView() @property (weak, nonatomic) UIView *contentView; @property (weak, nonatomic) UITableView *tableView; @property (strong, nonatomic) NSArray *dataArray; @end @implementation YXSelectRoomView - (id)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { NSMutableArray *listArray = [[NSMutableArray alloc]initWithCapacity:10]; for (YXHomeRoomModel *model in TheDataManager.rooms) { YXRoomItemModel *itemModel = [[YXRoomItemModel alloc]init]; itemModel.roomId = model.record_id; itemModel.title = model.name; itemModel.isSelected = NO; [listArray addObject:itemModel]; } self.dataArray = listArray; [self createCoverView]; [self createContentView]; } return self; } - (void)setRoomId:(NSString *)roomId { _roomId = roomId; for (YXRoomItemModel *model in self.dataArray) { if ([model.roomId isEqualToString:roomId]) { model.isSelected = YES; } } [self.tableView reloadData]; } -(void)createCoverView { UIView *coverView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)]; coverView.backgroundColor = [UIColor blackColor]; coverView.alpha = 0.3; [self addSubview:coverView]; } -(void)createContentView { UIView *contentView = [[UIView alloc]init]; contentView.backgroundColor = [UIColor colorWithHexString:@"#F9F9F9"]; [self addSubview:contentView]; self.contentView = contentView; [contentView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(@(SCREEN_HEIGHT/3.0)); make.left.equalTo(@0); make.right.equalTo(@0); make.bottom.equalTo(@0); }]; UILabel *titleLabel = [[UILabel alloc]init]; titleLabel.text = @"选择房间"; titleLabel.textColor = [UIColor colorWithHexString:@"#1E1E1E"]; titleLabel.font = [UIFont boldSystemFontOfSize:16]; titleLabel.textAlignment = NSTextAlignmentCenter; [contentView addSubview:titleLabel]; [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(@10); make.centerX.equalTo(contentView.mas_centerX); make.height.equalTo(@30); make.width.equalTo(@150); }]; UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [rightBtn setTitle:@"取消" forState:UIControlStateNormal]; [rightBtn addTarget:self action:@selector(rightBtnAction:) forControlEvents:UIControlEventTouchUpInside]; [rightBtn setTitleColor:[UIColor colorWithHexString:@"#267AFF"] forState:UIControlStateNormal]; rightBtn.titleLabel.font = [UIFont systemFontOfSize:15]; [contentView addSubview:rightBtn]; [rightBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(titleLabel.mas_top); make.right.equalTo(@-20); make.height.equalTo(@30); make.width.equalTo(@60); }]; UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10) style:UITableViewStyleGrouped]; tableView.delegate = self; tableView.dataSource = self; tableView.separatorStyle = UITableViewCellSeparatorStyleNone; [tableView registerNib:[UINib nibWithNibName:@"YXRoomItemTableViewCell" bundle:nil] forCellReuseIdentifier:@"roomItemCellID"]; tableView.backgroundColor = [UIColor colorWithHexString:@"#F9F9F9"]; [contentView addSubview:tableView]; self.tableView = tableView; [tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(titleLabel.mas_bottom).offset(10); make.right.equalTo(@0); make.left.equalTo(@0); make.bottom.equalTo(@(-kTabbarHeight-30)); }]; UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [sureBtn setTitle:@"确认" forState:UIControlStateNormal]; [sureBtn addTarget:self action:@selector(sureBtnAction:) forControlEvents:UIControlEventTouchUpInside]; [sureBtn setTitleColor:[UIColor colorWithHexString:@"#FFFFFF"] forState:UIControlStateNormal]; sureBtn.titleLabel.font = [UIFont systemFontOfSize:16]; sureBtn.backgroundColor = [UIColor colorWithHexString:@"#267AFF"]; [contentView addSubview:sureBtn]; [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.height.equalTo(@45); make.left.equalTo(@20); make.right.equalTo(@-20); make.bottom.equalTo(@(-kSafeBottomHeight)); }]; sureBtn.layer.cornerRadius = 22.5; } -(void)rightBtnAction:(UIButton *)btn { if (self.cancleBtnClick) { self.cancleBtnClick(self); } } -(void)sureBtnAction:(UIButton *)btn { NSString *roomId = @""; for (YXRoomItemModel *model in self.dataArray) { if (model.isSelected == YES) { roomId = model.roomId; } } if (self.sureBtnClick) { self.sureBtnClick(self,roomId); } } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataArray.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { YXRoomItemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"roomItemCellID" forIndexPath:indexPath]; cell.selectionStyle = UITableViewCellSelectionStyleNone; YXRoomItemModel *model = self.dataArray[indexPath.row]; cell.nameLabel.text = model.title; if (model.isSelected) { cell.selectedImgView.image = [UIImage imageNamed:@"home_device_selected"]; }else{ cell.selectedImgView.image = [UIImage imageNamed:@"home_device_unselected"]; } return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { for (YXRoomItemModel *model in self.dataArray) { model.isSelected = NO; } YXRoomItemModel *model = self.dataArray[indexPath.row]; model.isSelected = YES; [tableView reloadData]; } -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *headerView = [[UIView alloc]init]; return headerView; } -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { UIView *footerView = [[UIView alloc]init]; return footerView; } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 0.01; } -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return 0.01; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 65; } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ @end