ZCBaseTableVC.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //
  2. // ZCBaseTableVC.m
  3. // XLChat
  4. //
  5. // Created by 张灿 on 2017/10/24.
  6. // Copyright © 2017年 张灿. All rights reserved.
  7. //
  8. #import "ZCBaseTableVC.h"
  9. #import "UIScrollView+EmptyDataSet.h"
  10. @interface ZCBaseTableVC ()<DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>
  11. @end
  12. @interface ZCBaseTableVC ()
  13. @end
  14. @implementation ZCBaseTableVC
  15. - (void)viewDidLoad {
  16. [super viewDidLoad];
  17. self.isShowEmptyData = YES;
  18. self.isAllowScroll = NO;
  19. self.tableView.emptyDataSetSource = self;
  20. self.tableView.emptyDataSetDelegate = self;
  21. }
  22. -(UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
  23. {
  24. if (self.noDataImgName) {
  25. return [UIImage imageNamed:self.noDataImgName];
  26. }
  27. return [UIImage new]/*[UIImage imageNamed:@"vqu_images_air_no"]*/;
  28. }
  29. - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
  30. {
  31. NSString *text = self.noDataTitle?self.noDataTitle:@""/*@"暂无数据"*/;
  32. NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0f],
  33. NSForegroundColorAttributeName: HexColorFromRGB(0x6C6B70)};
  34. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  35. }
  36. - (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
  37. {
  38. NSString *text = self.noDataDetailTitle;
  39. NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
  40. paragraph.lineBreakMode = NSLineBreakByWordWrapping;
  41. paragraph.alignment = NSTextAlignmentCenter;
  42. NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:17.0f],
  43. NSForegroundColorAttributeName: [UIColor lightGrayColor],
  44. NSParagraphStyleAttributeName: paragraph};
  45. return self.noDataDetailTitle?[[NSAttributedString alloc] initWithString:text attributes:attributes]:nil;
  46. }
  47. - (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView{
  48. return self.isAllowScroll;
  49. }
  50. - (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView
  51. {
  52. return YES;
  53. }
  54. - (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView
  55. {
  56. return self.isShowEmptyData;
  57. }
  58. - (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView
  59. {
  60. // return (self.tableView.frame.size.height-50)/2-(self.tableView.frame.size.height-self.tableView.tableHeaderView.frame.size.height-50)/2;
  61. // if(self.tableView.tableHeaderView !=nil){
  62. // return self.tableView.tableHeaderView.frame.size.height-(self.tableView.frame.size.height-50-50)/2;//有tableHeaderview的时候距离tableHeaderView为50,还有一个50代表实际的图片,文字按钮加间距的高度总和
  63. // }else{
  64. // return 0;//没有tableHeaderView的时候在中间显示
  65. // }
  66. return 0;
  67. }
  68. - (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state
  69. {
  70. NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:16.0f]};
  71. return self.btnTitle?[[NSAttributedString alloc] initWithString:self.btnTitle attributes:attributes]:nil;
  72. }
  73. - (UIImage *)buttonImageForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state
  74. {
  75. return self.btnImgName?[UIImage imageNamed:self.btnImgName]:nil;
  76. }
  77. - (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button
  78. {
  79. [self buttonEvent];
  80. }
  81. - (nullable UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView{
  82. return [UIColor clearColor];
  83. }
  84. //解决和上下拉刷新控件一起显示时候的问题
  85. - (void)emptyDataSetWillAppear:(UIScrollView *)scrollView {
  86. scrollView.contentOffset = CGPointZero;
  87. }
  88. - (void)emptyDataSetDidAppear:(UIScrollView *)scrollView{
  89. scrollView.contentOffset = CGPointZero;
  90. }
  91. - (void)emptyDataSetWillDisappear:(UIScrollView *)scrollView{
  92. scrollView.contentOffset = CGPointZero;
  93. }
  94. /// 下拉刷新回调方法,具体实现代码需要子类实现
  95. - (void)headerRefreshing{
  96. }
  97. /// 上拉加载回调方法,具体实现代码需要子类实现
  98. - (void)footerRefreshing{
  99. }
  100. #pragma mark 按钮事件
  101. -(void)buttonEvent
  102. {
  103. }
  104. -(UITableView *)tableView
  105. {
  106. if(_tableView == nil)
  107. {
  108. _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NavBarHeight,[[UIScreen mainScreen] bounds].size.width,[[UIScreen mainScreen] bounds].size.height-NavBarHeight) style:UITableViewStyleGrouped];
  109. _tableView.backgroundColor = [UIColor clearColor];
  110. _tableView.tableFooterView = [UIView new];
  111. _tableView.tableHeaderView = [UIView new];
  112. _tableView.showsVerticalScrollIndicator = NO;
  113. _tableView.showsHorizontalScrollIndicator = NO;
  114. _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
  115. if (@available(iOS 15.0, *)) {
  116. _tableView.sectionHeaderTopPadding = 0;
  117. }
  118. }
  119. return _tableView;
  120. }
  121. -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
  122. {
  123. return 0.01f;
  124. }
  125. - (MJRefreshNormalHeader *)refreshHeader{
  126. if(!_refreshHeader){
  127. // 设置回调(一旦进入刷新状态,就调用target的action,也就是调用self的headerRefreshing方法)
  128. _refreshHeader = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefreshing)];
  129. }
  130. return _refreshHeader;
  131. }
  132. - (MJRefreshAutoNormalFooter *)loadMoreFooter{
  133. if(!_loadMoreFooter){
  134. // 设置回调(一旦进入刷新状态,就调用target的action,也就是调用self的footerRefreshing方法)
  135. _loadMoreFooter = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefreshing)];
  136. _loadMoreFooter.triggerAutomaticallyRefreshPercent = 0.1;
  137. }
  138. return _loadMoreFooter;
  139. }
  140. - (MJRefreshAutoNormalFooter *)noLoadMoreFooter{
  141. if(!_noLoadMoreFooter){
  142. _noLoadMoreFooter = [[MJRefreshAutoNormalFooter alloc]initWithFrame:CGRectZero];
  143. _noLoadMoreFooter.backgroundColor = [UIColor clearColor];
  144. _noLoadMoreFooter.state = MJRefreshStateIdle;
  145. _noLoadMoreFooter.mj_h = 0;//重新设置高度,不让看到它,并且当设置为table.footer时,table会自动调整自己的高度
  146. _noLoadMoreFooter.clipsToBounds = YES;//不让看不到菊花
  147. }
  148. return _noLoadMoreFooter;
  149. }
  150. @end