// // MOMyBackpackTableView.m // MiMoLive // // Created by SuperC on 2023/12/21. // //一页的size #define kPageSize 20 #import "MOMyBackpackTableView.h" @interface MOMyBackpackTableView () @property (nonatomic, strong) UITableView *tableView; /** 游标 */ @property (nonatomic, copy) NSString *next; /** 没有更多的数据视图 */ @property (nonatomic, strong) MONoMoreDataView *noMoreDataView; @end @implementation MOMyBackpackTableView - (instancetype)init{ self = [super init]; if (self) { [self setupUI]; } return self; } - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self setupUI]; } return self; } - (void)setupUI{ self.backgroundColor = [UIColor clearColor]; [self addSubview:self.tableView]; [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.left.right.bottom.equalTo(self); }]; } #pragma mark - JXCategoryListContentViewDelegate - (void)listWillAppear{ if(self.dataArr.count == 0){ if(self.category == 4){ self.next = @""; NSDictionary *basePage = @{@"size":@(kPageSize),@"next":self.next}; NSDictionary *baseDict = @{@"page":basePage}; [self getUserGiftBagDataWith:baseDict]; } else{ self.next = @""; NSDictionary *basePage = @{@"size":@(kPageSize),@"next":self.next}; NSDictionary *baseDict = @{@"category":@(self.category), @"page":basePage}; [self toGetMyBackpackListWithDict:baseDict]; } } } - (void)toGetFirstHttpData{ self.next = @""; NSDictionary *basePage = @{@"size":@(kPageSize),@"next":self.next}; NSDictionary *baseDict = @{@"category":@(self.category), @"page":basePage}; [self toGetMyBackpackListWithDict:baseDict]; } - (void)getUserGiftBagDataWith:(NSDictionary *)dict{ WEAKSELF [kHttpManager toGetThePackGiftAllWithParams:dict andBlock:^(id _Nonnull data, NSError * _Nonnull error) { [weakSelf.tableView.mj_footer endRefreshing]; if(kCode_Success){ MOGiftListBaseData *baseData = [MOGiftListBaseData modelObjectWithDictionary:data[@"data"]]; if(baseData.next == nil || baseData.next.length == 0){ if(weakSelf.next == nil || weakSelf.next.length == 0){ if(weakSelf.dataArr.count > 0){ [weakSelf.dataArr removeAllObjects]; } //第一页 weakSelf.dataArr = [baseData.giftlist mutableCopy]; } else{ //最后一页 [weakSelf.dataArr addObjectsFromArray:baseData.giftlist]; } weakSelf.next = @""; [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData]; } else{ weakSelf.next = baseData.next; [weakSelf.dataArr addObjectsFromArray:baseData.giftlist]; } for (MOGiftlist *object in weakSelf.dataArr) { object.isBag = YES; } [weakSelf.tableView reloadData]; weakSelf.tableView.mj_footer.hidden = (weakSelf.dataArr.count > 0) ? NO : YES; weakSelf.noMoreDataView.isHaveData = (weakSelf.dataArr.count > 0) ? YES : NO; } else{ kShowNetError(data) } }]; } - (void)toGetMyBackpackListWithDict:(NSDictionary *)dict{ WEAKSELF [kHttpManager toGetThePropUserListWithParams:dict andBlock:^(id _Nonnull data, NSError * _Nonnull error) { __strong typeof(weakSelf) self = weakSelf; [self.tableView.mj_footer endRefreshing]; if(kCode_Success){ // MOLogV(@"%@",[[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:data options:0 error:nil] encoding:NSUTF8StringEncoding]); MOShopBaseData *baseModel = [MOShopBaseData modelObjectWithDictionary:data[@"data"]]; if(baseModel.next == nil || baseModel.next.length == 0){ if(self.next == nil || self.next.length == 0){ //第一页 self.dataArr = [baseModel.shopList mutableCopy]; } else{ //最后一页 [self.dataArr addObjectsFromArray:baseModel.shopList]; } self.next = @""; [self.tableView.mj_footer endRefreshingWithNoMoreData]; } else{ self.next = baseModel.next; [self.dataArr addObjectsFromArray:baseModel.shopList]; } [self.tableView reloadData]; self.tableView.mj_footer.hidden = (self.dataArr.count > 0) ? NO : YES; self.noMoreDataView.isHaveData = (weakSelf.dataArr.count > 0) ? YES : NO; } else{ kShowNetError(data) } }]; } - (UIView *)listView{ return self; } #pragma mark - UITableViewDelegate,UITableViewDataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.dataArr.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ WEAKSELF if(self.category == 4){ MOGiftlist *modle = self.dataArr[indexPath.row]; MOMyBackpackCell *cell = [tableView dequeueReusableCellWithIdentifier:MOMyBackpackCell_ID]; cell.giftModel = modle; return cell; } else{ MOShopList *model = self.dataArr[indexPath.row]; MOMyBackpackCell *cell = [tableView dequeueReusableCellWithIdentifier:MOMyBackpackCell_ID]; cell.category = self.category; cell.cellModel = model; //设置成功回调 cell.switchValueChange = ^(MOShopList * _Nonnull cellModel) { for (MOShopList *tempObject in weakSelf.dataArr) { if([tempObject.id isEqualToString:cellModel.id]){ tempObject.used = cellModel.used; } else{ tempObject.used = NO; } } [weakSelf.tableView reloadData]; }; cell.buyShopView = ^(MOShopList * _Nonnull cellModel) { weakSelf.buyShopView ? weakSelf.buyShopView(cellModel) : nil; }; return cell; } } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [tableView deselectRowAtIndexPath:indexPath animated:YES]; } #pragma mark - Lazy - (UITableView *)tableView{ if (!_tableView) { _tableView= [[UITableView alloc] init]; if (@available(iOS 11.0, *)) { _tableView.estimatedRowHeight = 0; _tableView.estimatedSectionFooterHeight = 0; _tableView.estimatedSectionHeaderHeight = 0; _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } //iOS15适配 if (@available(iOS 15.0, *)) { self.tableView.sectionHeaderTopPadding = 0; } _tableView.backgroundColor = [UIColor clearColor]; _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; _tableView.dataSource = self; _tableView.delegate = self; _tableView.showsVerticalScrollIndicator = NO; _tableView.showsHorizontalScrollIndicator = NO; _tableView.rowHeight = 94.0; [_tableView registerNib:[UINib nibWithNibName:@"MOMyBackpackCell" bundle:nil] forCellReuseIdentifier:MOMyBackpackCell_ID]; WEAKSELF _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{ if(weakSelf.next.length == 0){ return; } if(weakSelf.category == 4){ NSDictionary *basePage = @{@"size":@(kPageSize),@"next":self.next}; NSDictionary *baseDict = @{@"page":basePage}; [self getUserGiftBagDataWith:baseDict]; } else{ NSDictionary *basePage = @{@"size":@(kPageSize),@"next":weakSelf.next}; NSDictionary *baseDict = @{@"category":@(weakSelf.category), @"page":basePage}; [weakSelf toGetMyBackpackListWithDict:baseDict]; } }]; _tableView.mj_footer.hidden = YES; _tableView.backgroundView = self.noMoreDataView; } return _tableView; } - (NSMutableArray *)dataArr{ if(!_dataArr){ _dataArr = [NSMutableArray array]; } return _dataArr; } - (MONoMoreDataView *)noMoreDataView{ if(!_noMoreDataView){ _noMoreDataView = [MONoMoreDataView new]; } return _noMoreDataView; } @end