YXSelectRoomView.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //
  2. // YXSelectRoomView.m
  3. // Temperature
  4. //
  5. // Created by TC on 2025/3/16.
  6. //
  7. #import "YXSelectRoomView.h"
  8. @interface YXSelectRoomView()
  9. @property (weak, nonatomic) UIView *contentView;
  10. @property (weak, nonatomic) UITableView *tableView;
  11. @property (strong, nonatomic) NSArray *dataArray;
  12. @end
  13. @implementation YXSelectRoomView
  14. - (id)initWithFrame:(CGRect)frame
  15. {
  16. if (self = [super initWithFrame:frame])
  17. {
  18. NSMutableArray *listArray = [[NSMutableArray alloc]initWithCapacity:10];
  19. for (YXHomeRoomModel *model in TheDataManager.rooms) {
  20. YXRoomItemModel *itemModel = [[YXRoomItemModel alloc]init];
  21. itemModel.roomId = model.record_id;
  22. itemModel.title = model.name;
  23. itemModel.isSelected = NO;
  24. [listArray addObject:itemModel];
  25. }
  26. self.dataArray = listArray;
  27. [self createCoverView];
  28. [self createContentView];
  29. }
  30. return self;
  31. }
  32. - (void)setRoomId:(NSString *)roomId
  33. {
  34. _roomId = roomId;
  35. for (YXRoomItemModel *model in self.dataArray) {
  36. if ([model.roomId isEqualToString:roomId]) {
  37. model.isSelected = YES;
  38. }
  39. }
  40. [self.tableView reloadData];
  41. }
  42. -(void)createCoverView
  43. {
  44. UIView *coverView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
  45. coverView.backgroundColor = [UIColor blackColor];
  46. coverView.alpha = 0.3;
  47. [self addSubview:coverView];
  48. }
  49. -(void)createContentView
  50. {
  51. UIView *contentView = [[UIView alloc]init];
  52. contentView.backgroundColor = [UIColor colorWithHexString:@"#F9F9F9"];
  53. [self addSubview:contentView];
  54. self.contentView = contentView;
  55. [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
  56. make.top.equalTo(@(SCREEN_HEIGHT/3.0));
  57. make.left.equalTo(@0);
  58. make.right.equalTo(@0);
  59. make.bottom.equalTo(@0);
  60. }];
  61. UILabel *titleLabel = [[UILabel alloc]init];
  62. titleLabel.text = @"选择房间";
  63. titleLabel.textColor = [UIColor colorWithHexString:@"#1E1E1E"];
  64. titleLabel.font = [UIFont boldSystemFontOfSize:16];
  65. titleLabel.textAlignment = NSTextAlignmentCenter;
  66. [contentView addSubview:titleLabel];
  67. [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  68. make.top.equalTo(@10);
  69. make.centerX.equalTo(contentView.mas_centerX);
  70. make.height.equalTo(@30);
  71. make.width.equalTo(@150);
  72. }];
  73. UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  74. [rightBtn setTitle:@"取消" forState:UIControlStateNormal];
  75. [rightBtn addTarget:self action:@selector(rightBtnAction:) forControlEvents:UIControlEventTouchUpInside];
  76. [rightBtn setTitleColor:[UIColor colorWithHexString:@"#267AFF"] forState:UIControlStateNormal];
  77. rightBtn.titleLabel.font = [UIFont systemFontOfSize:15];
  78. [contentView addSubview:rightBtn];
  79. [rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  80. make.top.equalTo(titleLabel.mas_top);
  81. make.right.equalTo(@-20);
  82. make.height.equalTo(@30);
  83. make.width.equalTo(@60);
  84. }];
  85. UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10) style:UITableViewStyleGrouped];
  86. tableView.delegate = self;
  87. tableView.dataSource = self;
  88. tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  89. [tableView registerNib:[UINib nibWithNibName:@"YXRoomItemTableViewCell" bundle:nil] forCellReuseIdentifier:@"roomItemCellID"];
  90. tableView.backgroundColor = [UIColor colorWithHexString:@"#F9F9F9"];
  91. [contentView addSubview:tableView];
  92. self.tableView = tableView;
  93. [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
  94. make.top.equalTo(titleLabel.mas_bottom).offset(10);
  95. make.right.equalTo(@0);
  96. make.left.equalTo(@0);
  97. make.bottom.equalTo(@(-kTabbarHeight-30));
  98. }];
  99. UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  100. [sureBtn setTitle:@"确认" forState:UIControlStateNormal];
  101. [sureBtn addTarget:self action:@selector(sureBtnAction:) forControlEvents:UIControlEventTouchUpInside];
  102. [sureBtn setTitleColor:[UIColor colorWithHexString:@"#FFFFFF"] forState:UIControlStateNormal];
  103. sureBtn.titleLabel.font = [UIFont systemFontOfSize:16];
  104. sureBtn.backgroundColor = [UIColor colorWithHexString:@"#267AFF"];
  105. [contentView addSubview:sureBtn];
  106. [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  107. make.height.equalTo(@45);
  108. make.left.equalTo(@20);
  109. make.right.equalTo(@-20);
  110. make.bottom.equalTo(@(-kSafeBottomHeight));
  111. }];
  112. sureBtn.layer.cornerRadius = 22.5;
  113. }
  114. -(void)rightBtnAction:(UIButton *)btn
  115. {
  116. if (self.cancleBtnClick) {
  117. self.cancleBtnClick(self);
  118. }
  119. }
  120. -(void)sureBtnAction:(UIButton *)btn
  121. {
  122. NSString *roomId = @"";
  123. for (YXRoomItemModel *model in self.dataArray) {
  124. if (model.isSelected == YES) {
  125. roomId = model.roomId;
  126. }
  127. }
  128. if (self.sureBtnClick) {
  129. self.sureBtnClick(self,roomId);
  130. }
  131. }
  132. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  133. {
  134. return 1;
  135. }
  136. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  137. {
  138. return self.dataArray.count;
  139. }
  140. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  141. {
  142. YXRoomItemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"roomItemCellID" forIndexPath:indexPath];
  143. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  144. YXRoomItemModel *model = self.dataArray[indexPath.row];
  145. cell.nameLabel.text = model.title;
  146. if (model.isSelected) {
  147. cell.selectedImgView.image = [UIImage imageNamed:@"home_device_selected"];
  148. }else{
  149. cell.selectedImgView.image = [UIImage imageNamed:@"home_device_unselected"];
  150. }
  151. return cell;
  152. }
  153. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  154. {
  155. for (YXRoomItemModel *model in self.dataArray) {
  156. model.isSelected = NO;
  157. }
  158. YXRoomItemModel *model = self.dataArray[indexPath.row];
  159. model.isSelected = YES;
  160. [tableView reloadData];
  161. }
  162. -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  163. {
  164. UIView *headerView = [[UIView alloc]init];
  165. return headerView;
  166. }
  167. -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
  168. {
  169. UIView *footerView = [[UIView alloc]init];
  170. return footerView;
  171. }
  172. -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  173. {
  174. return 0.01;
  175. }
  176. -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
  177. {
  178. return 0.01;
  179. }
  180. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  181. {
  182. return 65;
  183. }
  184. /*
  185. // Only override drawRect: if you perform custom drawing.
  186. // An empty implementation adversely affects performance during animation.
  187. - (void)drawRect:(CGRect)rect {
  188. // Drawing code
  189. }
  190. */
  191. @end