MACTableView.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. //
  2. // MACTableView.m
  3. // WeSchoolTeacher
  4. //
  5. // Created by MacKun on 16/3/1.
  6. // Copyright © 2016年 solloun. All rights reserved.
  7. //
  8. #import "MACTableView.h"
  9. #import "MACRefreshHeader.h"
  10. #import "MJRefreshAutoFooter.h"
  11. @interface MACTableView()<DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>{
  12. }
  13. /**
  14. * 是否第一次加载就展示空白页 默认为YES
  15. */
  16. @property(nonatomic,assign)BOOL firstShowEmpty;
  17. @end
  18. @implementation MACTableView
  19. -(instancetype)initWithCoder:(NSCoder *)aDecoder{
  20. self=[super initWithCoder:aDecoder];
  21. if (self) {
  22. [self initUI];
  23. }
  24. return self;
  25. }
  26. -(instancetype)initWithFrame:(CGRect)frame{
  27. self =[super initWithFrame:frame];
  28. if (self) {
  29. [self initUI];
  30. self.firstShowEmpty = NO;
  31. }
  32. return self;
  33. }
  34. -(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
  35. self =[super initWithFrame:frame style:style];
  36. if (self) {
  37. [self initUI];
  38. self.firstShowEmpty = NO;
  39. }
  40. return self;
  41. }
  42. -(void)initUI{
  43. self.tableFooterView = [UIView new];
  44. self.titleForEmpty = @"咋没数据呢,刷新试试~~";
  45. self.descriptionForEmpty = @"您的数据被程序猿搬走咯~~";
  46. self.imageNameForEmpty = @"placeholder_dropbox";
  47. self.firstShowEmpty = NO;
  48. self.isRefresh = YES;
  49. self.isLoadMore = YES;
  50. self.isShowEmpty = NO;
  51. self.showsHorizontalScrollIndicator = NO;
  52. self.showsVerticalScrollIndicator = NO;
  53. self.page = 0;
  54. }
  55. -(void)setMacTableViewDelegate:(id<MACTableViewDelegate>)macTableViewDelegate{
  56. _macTableViewDelegate = macTableViewDelegate;
  57. self.dataSource = (id<UITableViewDataSource>)macTableViewDelegate;
  58. self.delegate = (id<UITableViewDelegate>)macTableViewDelegate;
  59. }
  60. -(void)setIsRefresh:(BOOL)isRefresh{
  61. _isRefresh = isRefresh;
  62. if (isRefresh) {
  63. // header
  64. [self setMj_header:
  65. [MJRefreshStateHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshData)]];
  66. [self.mj_header setMultipleTouchEnabled:NO];
  67. }else{
  68. [self setMj_header:nil];
  69. }
  70. }
  71. -(void)setIsShowEmpty:(BOOL)isShowEmpty{
  72. _isShowEmpty = isShowEmpty;
  73. if (isShowEmpty) {
  74. self.emptyDataSetDelegate = self;
  75. self.emptyDataSetSource = self;
  76. }
  77. }
  78. -(void)setIsLoadMore:(BOOL)isLoadMore{
  79. _isLoadMore = isLoadMore;
  80. if (isLoadMore) {
  81. if (self.mj_footer==nil) {
  82. [self setMj_footer:[MJRefreshAutoNormalFooter
  83. footerWithRefreshingBlock:^{
  84. // 加载下一页在这
  85. // [self showWaiting];
  86. if (_macTableViewDelegate && [_macTableViewDelegate respondsToSelector:@selector(loadDataRefreshOrPull:)]) {
  87. [_macTableViewDelegate loadDataRefreshOrPull:MACPulling];
  88. }
  89. }]
  90. ];
  91. }
  92. [self.mj_footer setMultipleTouchEnabled:NO];
  93. }else{
  94. [self setMj_footer:nil];
  95. }
  96. }
  97. -(void)beginLoading{
  98. [self.mj_header beginRefreshing];
  99. }
  100. -(void)endLoading{
  101. if([self.mj_header isRefreshing])
  102. [self.mj_header endRefreshing];
  103. if ([self.mj_footer isRefreshing])
  104. [self.mj_footer endRefreshing];
  105. }
  106. -(void)refreshData{
  107. // [self showWaiting];
  108. if (self.mj_footer.state == MJRefreshStateNoMoreData) {
  109. [self.mj_footer resetNoMoreData];
  110. }
  111. if (_macTableViewDelegate && [_macTableViewDelegate respondsToSelector:@selector(loadDataRefreshOrPull:)]) {
  112. self.page = 0;
  113. [_macTableViewDelegate loadDataRefreshOrPull:MACRefreshing];
  114. }
  115. }
  116. -(NSNumber *)getCurrentPage{
  117. return [NSNumber numberWithInteger:++self.page];
  118. }
  119. #pragma mark - DZNEmptyDataSetSource Methods
  120. - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
  121. {
  122. NSString *text = self.titleForEmpty;
  123. NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:18.0f],
  124. NSForegroundColorAttributeName: [UIColor darkGrayColor]};
  125. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  126. }
  127. - (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
  128. {
  129. NSString *text = self.descriptionForEmpty;
  130. NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
  131. paragraph.lineBreakMode = NSLineBreakByWordWrapping;
  132. paragraph.alignment = NSTextAlignmentCenter;
  133. NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0f],
  134. NSForegroundColorAttributeName: [UIColor lightGrayColor],
  135. NSParagraphStyleAttributeName: paragraph};
  136. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  137. }
  138. - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
  139. {
  140. // if (!self.imageNameForEmpty || [self.imageNameForEmpty isBlank]) {
  141. // return nil;
  142. // }
  143. return [UIImage imageNamed:self.imageNameForEmpty];
  144. }
  145. - (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView
  146. {
  147. return [UIColor whiteColor];
  148. }
  149. - (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView
  150. {
  151. return -20.0f;
  152. }
  153. - (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView
  154. {
  155. return 0.0f;
  156. }
  157. #pragma mark - DZNEmptyDataSetDelegate Methods
  158. - (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView
  159. {
  160. if (self.firstShowEmpty) {
  161. self.firstShowEmpty = NO;
  162. return NO;
  163. }
  164. return YES;
  165. }
  166. - (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView
  167. {
  168. return YES;
  169. }
  170. - (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView
  171. {
  172. return YES;
  173. }
  174. - (CAAnimation *)imageAnimationForEmptyDataSet:(UIScrollView *)scrollView
  175. {
  176. CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath: @"transform"];
  177. animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
  178. animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_2, 0.0, 0.0, 1.0)];
  179. animation.duration = 0.25;
  180. animation.cumulative = YES;
  181. animation.repeatCount = MAXFLOAT;
  182. return animation;
  183. }
  184. @end