EMRefreshViewController.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. //
  2. // EMRefreshViewController.m
  3. // DXStudio
  4. //
  5. // Created by XieYajie on 12/10/2017.
  6. // Copyright © 2017 dxstudio. All rights reserved.
  7. //
  8. #import "EMRefreshViewController.h"
  9. #import "MJRefresh.h"
  10. @interface EMRefreshViewController ()
  11. @end
  12. @implementation EMRefreshViewController
  13. - (void)viewDidLoad {
  14. [super viewDidLoad];
  15. // Uncomment the following line to preserve selection between presentations.
  16. if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
  17. [self setEdgesForExtendedLayout:UIRectEdgeNone];
  18. }
  19. _page = 0;
  20. _showRefreshHeader = NO;
  21. _showRefreshFooter = NO;
  22. self.view.backgroundColor = [UIColor whiteColor];
  23. [self.view addSubview:self.tableView];
  24. }
  25. - (void)didReceiveMemoryWarning {
  26. [super didReceiveMemoryWarning];
  27. // Dispose of any resources that can be recreated.
  28. }
  29. #pragma mark - setter
  30. //- (void)setRefreshHeaderColor:(UIColor *)aColor
  31. //{
  32. // if (_showRefreshHeader && self.tableView.mj_header) {
  33. // [(MJRefreshNormalHeader *)self.tableView.mj_header setRefreshHeaderColor:aColor];
  34. // }
  35. //}
  36. - (void)setShowRefreshHeader:(BOOL)showRefreshHeader
  37. {
  38. if (_showRefreshHeader != showRefreshHeader) {
  39. _showRefreshHeader = showRefreshHeader;
  40. if (_showRefreshHeader) {
  41. __weak typeof(self) weakSelf = self;
  42. self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
  43. [weakSelf tableViewDidTriggerHeaderRefresh];
  44. }];
  45. self.tableView.mj_header.accessibilityIdentifier = @"refresh_header";
  46. // header.updatedTimeHidden = YES;
  47. }
  48. else{
  49. [self.tableView setMj_header:nil];
  50. }
  51. }
  52. }
  53. - (void)setShowRefreshFooter:(BOOL)showRefreshFooter
  54. {
  55. if (_showRefreshFooter != showRefreshFooter) {
  56. _showRefreshFooter = showRefreshFooter;
  57. if (_showRefreshFooter) {
  58. __weak typeof(self) weakSelf = self;
  59. self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
  60. [weakSelf tableViewDidTriggerFooterRefresh];
  61. }];
  62. self.tableView.mj_footer.accessibilityIdentifier = @"refresh_footer";
  63. }
  64. else{
  65. [self.tableView setMj_footer:nil];
  66. }
  67. }
  68. }
  69. #pragma mark - getter
  70. - (NSMutableArray *)dataArray
  71. {
  72. if (_dataArray == nil) {
  73. _dataArray = [NSMutableArray array];
  74. }
  75. return _dataArray;
  76. }
  77. - (UIView *)defaultFooterView
  78. {
  79. if (_defaultFooterView == nil) {
  80. _defaultFooterView = [[UIView alloc] init];
  81. }
  82. return _defaultFooterView;
  83. }
  84. - (UITableView *)tableView
  85. {
  86. if (_tableView == nil) {
  87. _tableView = [[UITableView alloc] init];
  88. _tableView.delegate = self;
  89. _tableView.dataSource = self;
  90. _tableView.tableFooterView = self.defaultFooterView;
  91. }
  92. return _tableView;
  93. }
  94. - (BOOL)isHeaderRefreshing
  95. {
  96. BOOL isRefreshing = NO;
  97. if (self.showRefreshHeader) {
  98. isRefreshing = self.tableView.mj_header.isRefreshing;
  99. }
  100. return isRefreshing;
  101. }
  102. - (BOOL)isFooterRefreshing
  103. {
  104. BOOL isRefreshing = NO;
  105. if (self.showRefreshFooter) {
  106. isRefreshing = self.tableView.mj_footer.isRefreshing;
  107. }
  108. return isRefreshing;
  109. }
  110. #pragma mark - Table view data source
  111. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  112. {
  113. // Return the number of sections.
  114. return 1;
  115. }
  116. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  117. {
  118. // Return the number of rows in the section.
  119. return 0;
  120. }
  121. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  122. {
  123. static NSString *CellIdentifier = @"UITableViewCell";
  124. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  125. // Configure the cell...
  126. return cell;
  127. }
  128. #pragma mark - public refresh
  129. - (void)autoTriggerHeaderRefresh
  130. {
  131. if (self.showRefreshHeader) {
  132. [self tableViewDidTriggerHeaderRefresh];
  133. }
  134. }
  135. - (void)tableViewDidTriggerHeaderRefresh
  136. {
  137. }
  138. - (void)tableViewDidTriggerFooterRefresh
  139. {
  140. }
  141. - (void)tableViewDidFinishTriggerHeader:(BOOL)isHeader reload:(BOOL)reload
  142. {
  143. __weak typeof(self) weakSelf = self;
  144. dispatch_async(dispatch_get_main_queue(), ^{
  145. if (reload) {
  146. [weakSelf.tableView reloadData];
  147. }
  148. if (isHeader) {
  149. [weakSelf.tableView.mj_header endRefreshing];
  150. }
  151. else{
  152. [weakSelf.tableView.mj_footer endRefreshing];
  153. }
  154. });
  155. }
  156. @end