BaseTableVC.m 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //
  2. // BaseTableVC.m
  3. //中车运
  4. //
  5. // Created by 隋林栋 on 2016/12/14.
  6. // Copyright © 2016年 ping. All rights reserved.
  7. //
  8. #import "BaseTableVC.h"
  9. //refresh header
  10. //#import "MJChiBaoZiHeader.h"
  11. //refresh footer
  12. #import "CutomFooter.h"
  13. //keyboard
  14. //tableVC category
  15. #import "BaseTableVC+KeyboardObserve.h"
  16. @interface BaseTableVC ()
  17. @property (nonatomic, strong) UIView *tableBackgroundView;//table bg
  18. @end
  19. @implementation BaseTableVC
  20. #pragma mark lazy init
  21. - (NSMutableArray *)aryDatas{
  22. if (!_aryDatas) {
  23. _aryDatas = [NSMutableArray array];
  24. }
  25. return _aryDatas;
  26. }
  27. - (NSString *)strCellName{//关联无数据加载
  28. return nil;
  29. }
  30. - (NSString *)strModelName{//关联本地数据加载
  31. return nil;
  32. }
  33. - (NSString *)strLocalKey{
  34. return NSStringFromClass([self class]);
  35. }
  36. - (double)pageNum{
  37. if (self.isRemoveAll) {
  38. _pageNum = 1;
  39. }
  40. return _pageNum;
  41. }
  42. - (UITableView *)tableView{
  43. if (!_tableView) {
  44. _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, NAVIGATIONBAR_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT - NAVIGATIONBAR_HEIGHT) style:UITableViewStyleGrouped];
  45. if (@available(iOS 11.0, *)) {
  46. _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  47. _tableView.estimatedRowHeight = 0;
  48. _tableView.estimatedSectionFooterHeight = 0;
  49. _tableView.estimatedSectionHeaderHeight = 0;
  50. }
  51. _tableView.delegate = self;
  52. _tableView.dataSource = self;
  53. _tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
  54. _tableView.backgroundColor = [UIColor whiteColor];
  55. _tableView.showsVerticalScrollIndicator = NO;
  56. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  57. [_tableView addSubview:self.tableBackgroundView];
  58. if (self.strCellName != nil) {
  59. [_tableView registerClass:NSClassFromString(self.strCellName) forCellReuseIdentifier:self.strCellName];
  60. }
  61. }
  62. return _tableView;
  63. }
  64. - (UIView *)tableBackgroundView{
  65. if (!_tableBackgroundView) {
  66. _tableBackgroundView = ^(){
  67. UIView * viewBg = [UIView new];
  68. viewBg.frame =CGRectMake(0, -W(200), SCREEN_WIDTH, W(200));
  69. viewBg.backgroundColor = COLOR_BACKGROUND;
  70. viewBg.tag = TAG_LINE;
  71. return viewBg;
  72. }();
  73. }
  74. return _tableBackgroundView;
  75. }
  76. - (NSString *)lastRow{
  77. if (isAry(self.aryDatas) && !self.isRemoveAll) {
  78. id model = self.aryDatas.lastObject;
  79. if ([model respondsToSelector:NSSelectorFromString(@"row")]) {
  80. id row = [model valueForKeyPath:@"row"];
  81. return [NSString stringWithFormat:@"%@",row];
  82. }
  83. }
  84. return @"0";
  85. }
  86. - (NSString *)lastUpdateTime{
  87. if (isAry(self.aryDatas) && !self.isRemoveAll) {
  88. id model = self.aryDatas.lastObject;
  89. if ([model respondsToSelector:NSSelectorFromString(@"updDate")]) {
  90. id update = [model valueForKeyPath:@"updDate"];
  91. if (update != nil) {
  92. return [NSString stringWithFormat:@"%@",update];
  93. }
  94. }
  95. }
  96. return @"";
  97. }
  98. - (id)requestDelegate{
  99. self.isNotShowLoadingView = isAry(self.aryDatas) && self.isRemoveAll;
  100. return self;
  101. }
  102. #pragma mark 增加上拉 下拉
  103. - (void)addRefresh{
  104. [self addRefreshHeader];
  105. [self addRefreshFooter];
  106. }
  107. - (void)addRefreshHeader{
  108. self.tableView.mj_header = [MJRefreshHeader new];
  109. [self.tableView.mj_header setRefreshingTarget:self refreshingAction:@selector(refreshHeaderAll)];
  110. [self.tableView insertSubview:self.tableBackgroundView atIndex:0];
  111. }
  112. - (void)addRefreshFooter{
  113. self.tableView.mj_footer = [[CutomFooter alloc]init];
  114. [self.tableView.mj_footer setRefreshingTarget:self refreshingAction:@selector(refreshFooterAll)];
  115. }
  116. #pragma mark 上拉 下拉
  117. - (void)refreshHeaderAll{
  118. self.tableView.mj_footer.userInteractionEnabled = false;
  119. self.isRemoveAll = true;
  120. [self requestList];
  121. }
  122. - (void)refreshFooterAll{
  123. self.tableView.mj_header.userInteractionEnabled = false;
  124. self.isRemoveAll = false;
  125. [self requestList];
  126. }
  127. #pragma mark 结束上拉 下拉
  128. - (void)endRefreshing{
  129. self.tableView.mj_header.userInteractionEnabled = true;
  130. self.tableView.mj_footer.userInteractionEnabled = true;
  131. [self.tableView.mj_header endRefreshing];
  132. [self.tableView.mj_footer endRefreshing];
  133. }
  134. #pragma mark init
  135. - (instancetype)init{
  136. self = [super init];
  137. if (self) {
  138. self.isRemoveAll = true;
  139. }
  140. return self;
  141. }
  142. #pragma mark view did load
  143. - (void)viewDidLoad {
  144. [super viewDidLoad];
  145. self.aryDatas = [NSMutableArray array];
  146. [self.view addSubview:self.tableView];
  147. }
  148. #pragma mark table delegate
  149. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  150. return self.aryDatas.count;
  151. }
  152. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  153. if (self.strCellName) {
  154. id cell = [tableView dequeueReusableCellWithIdentifier:self.strCellName forIndexPath:indexPath];
  155. [GlobalMethod performSelector:@"resetCellWithModel:" delegate:cell object:self.aryDatas[indexPath.row] isHasReturn:false];
  156. return cell;
  157. }
  158. return ^(){
  159. UITableViewCell * cell = [self.tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
  160. if (!cell) {
  161. cell = [[UITableViewCell alloc]init];
  162. }
  163. return cell;
  164. }();
  165. }
  166. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  167. return isStr(self.strCellName)?[NSClassFromString(self.strCellName) fetchHeight:self.aryDatas[indexPath.row]]:CGFLOAT_MIN;
  168. }
  169. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
  170. return CGFLOAT_MIN;
  171. }
  172. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  173. return CGFLOAT_MIN;
  174. }
  175. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
  176. return [UIView new];
  177. }
  178. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
  179. return [UIView new];
  180. }
  181. #pragma mark request
  182. - (void)requestList{
  183. }
  184. #pragma mark noresult after requst
  185. - (void)showNoResult{
  186. [self.noResultLoadingView removeFromSuperview];
  187. [self.noResultView removeFromSuperview];
  188. if(!self.isShowNoResult)return;
  189. if (self.aryDatas.count == 0) {
  190. CGFloat top = 0;
  191. if (self.tableView.tableHeaderView != nil) {
  192. top = self.tableView.tableHeaderView.height;
  193. }
  194. [self.noResultView showInView:self.tableView frame:CGRectMake(0, top, self.tableView.width, self.tableView.height)];
  195. }
  196. }
  197. #pragma mark noresult before request
  198. - (void)showNoResultLoadingView{
  199. [self.noResultLoadingView removeFromSuperview];
  200. if(!self.isShowNoResultLoadingView)return;
  201. if (self.aryDatas.count == 0) {
  202. CGFloat top = 0;
  203. if (self.tableView.tableHeaderView != nil) {
  204. top = self.tableView.tableHeaderView.height;
  205. }
  206. [self.noResultLoadingView showInView:self.tableView frame:CGRectMake(0, top, self.tableView.width, self.tableView.height)];
  207. }
  208. }
  209. @end