MOMyBackpackTableView.m 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. //
  2. // MOMyBackpackTableView.m
  3. // MiMoLive
  4. //
  5. // Created by SuperC on 2023/12/21.
  6. //
  7. //一页的size
  8. #define kPageSize 20
  9. #import "MOMyBackpackTableView.h"
  10. @interface MOMyBackpackTableView ()<UITableViewDelegate,UITableViewDataSource>
  11. @property (nonatomic, strong) UITableView *tableView;
  12. /** 游标 */
  13. @property (nonatomic, copy) NSString *next;
  14. /** 没有更多的数据视图 */
  15. @property (nonatomic, strong) MONoMoreDataView *noMoreDataView;
  16. @end
  17. @implementation MOMyBackpackTableView
  18. - (instancetype)init{
  19. self = [super init];
  20. if (self)
  21. {
  22. [self setupUI];
  23. }
  24. return self;
  25. }
  26. - (instancetype)initWithFrame:(CGRect)frame
  27. {
  28. self = [super initWithFrame:frame];
  29. if (self)
  30. {
  31. [self setupUI];
  32. }
  33. return self;
  34. }
  35. - (void)setupUI{
  36. self.backgroundColor = [UIColor clearColor];
  37. [self addSubview:self.tableView];
  38. [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
  39. make.top.left.right.bottom.equalTo(self);
  40. }];
  41. }
  42. #pragma mark - JXCategoryListContentViewDelegate
  43. - (void)listWillAppear{
  44. if(self.dataArr.count == 0){
  45. if(self.category == 4){
  46. self.next = @"";
  47. NSDictionary *basePage = @{@"size":@(kPageSize),@"next":self.next};
  48. NSDictionary *baseDict = @{@"page":basePage};
  49. [self getUserGiftBagDataWith:baseDict];
  50. }
  51. else{
  52. self.next = @"";
  53. NSDictionary *basePage = @{@"size":@(kPageSize),@"next":self.next};
  54. NSDictionary *baseDict = @{@"category":@(self.category),
  55. @"page":basePage};
  56. [self toGetMyBackpackListWithDict:baseDict];
  57. }
  58. }
  59. }
  60. - (void)toGetFirstHttpData{
  61. self.next = @"";
  62. NSDictionary *basePage = @{@"size":@(kPageSize),@"next":self.next};
  63. NSDictionary *baseDict = @{@"category":@(self.category),
  64. @"page":basePage};
  65. [self toGetMyBackpackListWithDict:baseDict];
  66. }
  67. - (void)getUserGiftBagDataWith:(NSDictionary *)dict{
  68. WEAKSELF
  69. [kHttpManager toGetThePackGiftAllWithParams:dict andBlock:^(id _Nonnull data, NSError * _Nonnull error) {
  70. [weakSelf.tableView.mj_footer endRefreshing];
  71. if(kCode_Success){
  72. MOGiftListBaseData *baseData = [MOGiftListBaseData modelObjectWithDictionary:data[@"data"]];
  73. if(baseData.next == nil || baseData.next.length == 0){
  74. if(weakSelf.next == nil || weakSelf.next.length == 0){
  75. if(weakSelf.dataArr.count > 0){
  76. [weakSelf.dataArr removeAllObjects];
  77. }
  78. //第一页
  79. weakSelf.dataArr = [baseData.giftlist mutableCopy];
  80. }
  81. else{
  82. //最后一页
  83. [weakSelf.dataArr addObjectsFromArray:baseData.giftlist];
  84. }
  85. weakSelf.next = @"";
  86. [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
  87. }
  88. else{
  89. weakSelf.next = baseData.next;
  90. [weakSelf.dataArr addObjectsFromArray:baseData.giftlist];
  91. }
  92. for (MOGiftlist *object in weakSelf.dataArr) {
  93. object.isBag = YES;
  94. }
  95. [weakSelf.tableView reloadData];
  96. weakSelf.tableView.mj_footer.hidden = (weakSelf.dataArr.count > 0) ? NO : YES;
  97. weakSelf.noMoreDataView.isHaveData = (weakSelf.dataArr.count > 0) ? YES : NO;
  98. }
  99. else{
  100. kShowNetError(data)
  101. }
  102. }];
  103. }
  104. - (void)toGetMyBackpackListWithDict:(NSDictionary *)dict{
  105. WEAKSELF
  106. [kHttpManager toGetThePropUserListWithParams:dict andBlock:^(id _Nonnull data, NSError * _Nonnull error) {
  107. __strong typeof(weakSelf) self = weakSelf;
  108. [self.tableView.mj_footer endRefreshing];
  109. if(kCode_Success){
  110. // MOLogV(@"%@",[[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:data options:0 error:nil] encoding:NSUTF8StringEncoding]);
  111. MOShopBaseData *baseModel = [MOShopBaseData modelObjectWithDictionary:data[@"data"]];
  112. if(baseModel.next == nil || baseModel.next.length == 0){
  113. if(self.next == nil || self.next.length == 0){
  114. //第一页
  115. self.dataArr = [baseModel.shopList mutableCopy];
  116. }
  117. else{
  118. //最后一页
  119. [self.dataArr addObjectsFromArray:baseModel.shopList];
  120. }
  121. self.next = @"";
  122. [self.tableView.mj_footer endRefreshingWithNoMoreData];
  123. }
  124. else{
  125. self.next = baseModel.next;
  126. [self.dataArr addObjectsFromArray:baseModel.shopList];
  127. }
  128. [self.tableView reloadData];
  129. self.tableView.mj_footer.hidden = (self.dataArr.count > 0) ? NO : YES;
  130. self.noMoreDataView.isHaveData = (weakSelf.dataArr.count > 0) ? YES : NO;
  131. }
  132. else{
  133. kShowNetError(data)
  134. }
  135. }];
  136. }
  137. - (UIView *)listView{
  138. return self;
  139. }
  140. #pragma mark - UITableViewDelegate,UITableViewDataSource
  141. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  142. return 1;
  143. }
  144. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  145. return self.dataArr.count;
  146. }
  147. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  148. WEAKSELF
  149. if(self.category == 4){
  150. MOGiftlist *modle = self.dataArr[indexPath.row];
  151. MOMyBackpackCell *cell = [tableView dequeueReusableCellWithIdentifier:MOMyBackpackCell_ID];
  152. cell.giftModel = modle;
  153. return cell;
  154. }
  155. else{
  156. MOShopList *model = self.dataArr[indexPath.row];
  157. MOMyBackpackCell *cell = [tableView dequeueReusableCellWithIdentifier:MOMyBackpackCell_ID];
  158. cell.category = self.category;
  159. cell.cellModel = model;
  160. //设置成功回调
  161. cell.switchValueChange = ^(MOShopList * _Nonnull cellModel) {
  162. for (MOShopList *tempObject in weakSelf.dataArr) {
  163. if([tempObject.id isEqualToString:cellModel.id]){
  164. tempObject.used = cellModel.used;
  165. }
  166. else{
  167. tempObject.used = NO;
  168. }
  169. }
  170. [weakSelf.tableView reloadData];
  171. };
  172. cell.buyShopView = ^(MOShopList * _Nonnull cellModel) {
  173. weakSelf.buyShopView ? weakSelf.buyShopView(cellModel) : nil;
  174. };
  175. return cell;
  176. }
  177. }
  178. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  179. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  180. }
  181. #pragma mark - Lazy
  182. - (UITableView *)tableView{
  183. if (!_tableView)
  184. {
  185. _tableView= [[UITableView alloc] init];
  186. if (@available(iOS 11.0, *))
  187. {
  188. _tableView.estimatedRowHeight = 0;
  189. _tableView.estimatedSectionFooterHeight = 0;
  190. _tableView.estimatedSectionHeaderHeight = 0;
  191. _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  192. }
  193. //iOS15适配
  194. if (@available(iOS 15.0, *))
  195. {
  196. self.tableView.sectionHeaderTopPadding = 0;
  197. }
  198. _tableView.backgroundColor = [UIColor clearColor];
  199. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  200. _tableView.dataSource = self;
  201. _tableView.delegate = self;
  202. _tableView.showsVerticalScrollIndicator = NO;
  203. _tableView.showsHorizontalScrollIndicator = NO;
  204. _tableView.rowHeight = 94.0;
  205. [_tableView registerNib:[UINib nibWithNibName:@"MOMyBackpackCell" bundle:nil] forCellReuseIdentifier:MOMyBackpackCell_ID];
  206. WEAKSELF
  207. _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
  208. if(weakSelf.next.length == 0){
  209. return;
  210. }
  211. if(weakSelf.category == 4){
  212. NSDictionary *basePage = @{@"size":@(kPageSize),@"next":self.next};
  213. NSDictionary *baseDict = @{@"page":basePage};
  214. [self getUserGiftBagDataWith:baseDict];
  215. }
  216. else{
  217. NSDictionary *basePage = @{@"size":@(kPageSize),@"next":weakSelf.next};
  218. NSDictionary *baseDict = @{@"category":@(weakSelf.category),
  219. @"page":basePage};
  220. [weakSelf toGetMyBackpackListWithDict:baseDict];
  221. }
  222. }];
  223. _tableView.mj_footer.hidden = YES;
  224. _tableView.backgroundView = self.noMoreDataView;
  225. }
  226. return _tableView;
  227. }
  228. - (NSMutableArray *)dataArr{
  229. if(!_dataArr){
  230. _dataArr = [NSMutableArray array];
  231. }
  232. return _dataArr;
  233. }
  234. - (MONoMoreDataView *)noMoreDataView{
  235. if(!_noMoreDataView){
  236. _noMoreDataView = [MONoMoreDataView new];
  237. }
  238. return _noMoreDataView;
  239. }
  240. @end