123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 |
- //
- // YXDeviceViewController.m
- // Temperature
- //
- // Created by z on 2025/1/17.
- //
- #import "YXDeviceViewController.h"
- #import "YXDeviceCollectionViewCell.h"
- #import "YXDeviceListHeaderView.h"
- #import "LJCollectionViewFlowLayout.h"
- #import "YXLeftTableViewCell.h"
- #import <SDWebImage/SDWebImage.h>
- #import "NSObject+Property.h"
- //#import "RDSResetDeviceVC.h"
- #import "YXStepViewController.h"
- #import "YXDeviceListModel.h"
- #import "RDSRootControl.h"
- @interface YXDeviceViewController ()<UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegateFlowLayout,
- UICollectionViewDataSource>
- @property (nonatomic, strong) UITableView *tableView;
- @property (nonatomic, strong) UICollectionView *collectionView;
- @property (nonatomic, strong) NSMutableArray *dataSource;
- @property (nonatomic, strong) LJCollectionViewFlowLayout *flowLayout;
- @end
- static float kLeftTableViewWidth = 90.f;
- static float kCollectionViewMargin = 3.f;
- @implementation YXDeviceViewController
- {
- NSInteger _selectIndex;
- BOOL _isScrollDown;
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- self.navigationItem.title = @"添加设备";
- self.view.backgroundColor = [UIColor whiteColor];
-
- _selectIndex = 0;
- _isScrollDown = YES;
-
- self.edgesForExtendedLayout = UIRectEdgeNone;
- self.extendedLayoutIncludesOpaqueBars = NO;
- self.tableView.contentInsetAdjustmentBehavior = NO;
- self.collectionView.contentInsetAdjustmentBehavior = NO;
-
- self.dataSource = [[NSMutableArray alloc]initWithCapacity:5];
- [self getDeviceList];
-
- [self.view addSubview:self.tableView];
- [self.view addSubview:self.collectionView];
-
-
- }
- -(void)getDeviceList
- {
- [RDSDemoApiHelper rds_getDeviceTypeListSuccess:^(id responseObject) {
- NSLog(@"responseObject is %@", responseObject);
- if ([responseObject[@"code"] intValue] == 9999) {
- [RDSRootControl shareControl].isLoginSuccess = NO;
- TheDataManager.token = @"";
- }
-
- if ([responseObject[@"code"] integerValue] == 0) {
- NSArray *data = responseObject[@"data"];
-
- for (int i = 0; i < data.count; i++) {
- NSDictionary *listDic = data[i];
- YXDeviceListModel *listModel = [YXDeviceListModel mj_objectWithKeyValues:listDic];
- NSArray *itemArray = listDic[@"children"];
- NSMutableArray *itemModelArray = [[NSMutableArray alloc]initWithCapacity:5];
- for (int j = 0; j < itemArray.count; j++) {
- NSDictionary *itemDic = itemArray[j];
- YXDeviceItemModel *itemModel = [YXDeviceItemModel mj_objectWithKeyValues:itemDic];
-
- NSArray *stepArray = itemDic[@"steps"];
- NSArray *stepModelArray = [YXDeviceStepModel mj_objectArrayWithKeyValuesArray:stepArray];
- itemModel.steps = stepModelArray;
- [itemModelArray addObject:itemModel];
- }
-
- listModel.children = itemModelArray;
- [self->_dataSource addObject:listModel];
- }
-
- [self.tableView reloadData];
- [self.collectionView reloadData];
-
- [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
- }
- } failure:^(NSError *error) {
-
- }];
- }
- #pragma mark - Getters
- - (UITableView *)tableView
- {
- if (!_tableView)
- {
- _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kLeftTableViewWidth, SCREEN_HEIGHT)];
- _tableView.backgroundColor = [UIColor colorWithHexString:@"#F9F9F9"];
- _tableView.delegate = self;
- _tableView.dataSource = self;
- _tableView.tableFooterView = [UIView new];
- _tableView.rowHeight = 55;
- _tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
- _tableView.showsVerticalScrollIndicator = NO;
- _tableView.separatorColor = [UIColor clearColor];
- [_tableView registerClass:[YXLeftTableViewCell class] forCellReuseIdentifier:@"leftTableViewCellID"];
- }
- return _tableView;
- }
- - (LJCollectionViewFlowLayout *)flowLayout
- {
- if (!_flowLayout)
- {
- _flowLayout = [[LJCollectionViewFlowLayout alloc] init];
- _flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
- _flowLayout.minimumInteritemSpacing = 2;
- _flowLayout.minimumLineSpacing = 2;
- }
- return _flowLayout;
- }
- - (UICollectionView *)collectionView
- {
- if (!_collectionView)
- {
- _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(kCollectionViewMargin + kLeftTableViewWidth, kCollectionViewMargin, SCREEN_WIDTH - kLeftTableViewWidth - 2 * kCollectionViewMargin, SCREEN_HEIGHT - 2 * kCollectionViewMargin) collectionViewLayout:self.flowLayout];
- _collectionView.delegate = self;
- _collectionView.dataSource = self;
- _collectionView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
- _collectionView.showsVerticalScrollIndicator = NO;
- _collectionView.showsHorizontalScrollIndicator = NO;
- [_collectionView setBackgroundColor:[UIColor clearColor]];
- //注册cell
- [_collectionView registerClass:[YXDeviceCollectionViewCell class] forCellWithReuseIdentifier:@"deviceCollectionViewCellID"];
- //注册分区头标题
- [_collectionView registerClass:[YXDeviceListHeaderView class]
- forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
- withReuseIdentifier:@"headerViewID"];
- }
- return _collectionView;
- }
- #pragma mark - UITableView DataSource Delegate
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- return self.dataSource.count;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- YXLeftTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"leftTableViewCellID" forIndexPath:indexPath];
- YXDeviceListModel *model = self.dataSource[indexPath.row];
- cell.titleLabel.text = model.name;
- return cell;
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- _selectIndex = indexPath.row;
-
- // 解决点击 TableView 后 CollectionView 的 Header 遮挡问题。
- [self scrollToTopOfSection:_selectIndex animated:YES];
-
- // [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:_selectIndex] atScrollPosition:UICollectionViewScrollPositionTop animated:YES];
- [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:_selectIndex inSection:0]
- atScrollPosition:UITableViewScrollPositionTop animated:YES];
- }
- #pragma mark - 解决点击 TableView 后 CollectionView 的 Header 遮挡问题
- - (void)scrollToTopOfSection:(NSInteger)section animated:(BOOL)animated
- {
- CGRect headerRect = [self frameForHeaderForSection:section];
- CGPoint topOfHeader = CGPointMake(0, headerRect.origin.y - _collectionView.contentInset.top);
- [self.collectionView setContentOffset:topOfHeader animated:animated];
- }
- - (CGRect)frameForHeaderForSection:(NSInteger)section
- {
- NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:section];
- UICollectionViewLayoutAttributes *attributes = [self.collectionView.collectionViewLayout layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:indexPath];
- return attributes.frame;
- }
- #pragma mark - UICollectionView DataSource Delegate
- - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
- {
- return self.dataSource.count;
- }
- - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
- {
- YXDeviceListModel *listModel = self.dataSource[section];
- return listModel.children.count;
- }
- - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
- {
- YXDeviceCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"deviceCollectionViewCellID" forIndexPath:indexPath];
- YXDeviceListModel *listModel = self.dataSource[indexPath.section];
- YXDeviceItemModel *itemModel = listModel.children[indexPath.row];
-
- NSString *urlStr = [NSString stringWithFormat:@"%@%@",kBaseUrlHeadImg,itemModel.icon];
- [cell.picImgView sd_setImageWithURL:[NSURL URLWithString:urlStr]];
- cell.textLabel.text = itemModel.name;
-
- return cell;
- }
- - (CGSize)collectionView:(UICollectionView *)collectionView
- layout:(UICollectionViewLayout *)collectionViewLayout
- sizeForItemAtIndexPath:(NSIndexPath *)indexPath
- {
- return CGSizeMake((SCREEN_WIDTH - kLeftTableViewWidth - 4 * kCollectionViewMargin) / 3,
- (SCREEN_WIDTH - kLeftTableViewWidth - 4 * kCollectionViewMargin) / 3 + 30);
- }
- - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
- viewForSupplementaryElementOfKind:(NSString *)kind
- atIndexPath:(NSIndexPath *)indexPath
- {
- NSString *reuseIdentifier;
- if ([kind isEqualToString:UICollectionElementKindSectionHeader])
- { // header
- reuseIdentifier = @"CollectionViewHeaderView";
- }
- YXDeviceListHeaderView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headerViewID" forIndexPath:indexPath];
- if ([kind isEqualToString:UICollectionElementKindSectionHeader])
- {
- YXDeviceItemModel *model = self.dataSource[indexPath.section];
- view.titleLabel.text = model.name;
- }
- return view;
- }
- - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
- {
- return CGSizeMake(SCREEN_WIDTH, 30);
- }
- // CollectionView分区标题即将展示
- - (void)collectionView:(UICollectionView *)collectionView willDisplaySupplementaryView:(UICollectionReusableView *)view forElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath
- {
- // 当前CollectionView滚动的方向向上,CollectionView是用户拖拽而产生滚动的(主要是判断CollectionView是用户拖拽而滚动的,还是点击TableView而滚动的)
- if (!_isScrollDown && (collectionView.dragging || collectionView.decelerating))
- {
- [self selectRowAtIndexPath:indexPath.section];
- }
- }
- // CollectionView分区标题展示结束
- - (void)collectionView:(UICollectionView *)collectionView didEndDisplayingSupplementaryView:(nonnull UICollectionReusableView *)view forElementOfKind:(nonnull NSString *)elementKind atIndexPath:(nonnull NSIndexPath *)indexPath
- {
- // 当前CollectionView滚动的方向向下,CollectionView是用户拖拽而产生滚动的(主要是判断CollectionView是用户拖拽而滚动的,还是点击TableView而滚动的)
- if (_isScrollDown && (collectionView.dragging || collectionView.decelerating))
- {
- [self selectRowAtIndexPath:indexPath.section + 1];
- }
- }
- // 当拖动CollectionView的时候,处理TableView
- - (void)selectRowAtIndexPath:(NSInteger)index
- {
- [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] animated:YES scrollPosition:UITableViewScrollPositionMiddle];
- }
- -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
- {
- YXDeviceListModel *listModel = self.dataSource[indexPath.section];
- YXDeviceItemModel *itemModel = listModel.children[indexPath.row];
-
-
- YXStepViewController *stepVC = [[YXStepViewController alloc] init];
- stepVC.itemModel = itemModel;
- [self pushViewController:stepVC animated:YES];
-
-
- // RDSResetDeviceVC *resetDeviceVC = [[RDSResetDeviceVC alloc] init];
- // resetDeviceVC.itemModel = itemModel;
- // [self pushViewController:resetDeviceVC animated:YES];
- }
- #pragma mark - UIScrollView Delegate
- // 标记一下CollectionView的滚动方向,是向上还是向下
- - (void)scrollViewDidScroll:(UIScrollView *)scrollView
- {
- static float lastOffsetY = 0;
- if (self.collectionView == scrollView)
- {
- _isScrollDown = lastOffsetY < scrollView.contentOffset.y;
- lastOffsetY = scrollView.contentOffset.y;
- }
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- /*
- #pragma mark - Navigation
- // In a storyboard-based application, you will often want to do a little preparation before navigation
- - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
- // Get the new view controller using [segue destinationViewController].
- // Pass the selected object to the new view controller.
- }
- */
- @end
|