天天省钱快报

UITableView+MJRefreshAutoManager.m 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // UITableView+MJRefreshAutoManager.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/5/23.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "UITableView+MJRefreshAutoManager.h"
  9. #define NO_MORE_DATA_TEXT @"无更多数据了"
  10. @implementation UITableView (MJRefreshAutoManager)
  11. static char stateKey;
  12. - (void)setFootRefreshState:(MJTableFooterRefreshState)footRefreshState {
  13. UIWindow *window = [UIApplication sharedApplication].keyWindow;
  14. [RACObserve(self.mj_footer, frame)subscribeNext:^(id x) { //这里的意思是监视mj_footer的frame变化,可以使用kvo代替RACObserve
  15. CGPoint point = [self convertPoint:self.mj_footer.frame.origin toView:window];
  16. if (point.y < window.frame.size.height) {
  17. [(MJRefreshAutoNormalFooter *)self.mj_footer setTitle:@"" forState:MJRefreshStateIdle];
  18. }else {
  19. [(MJRefreshAutoNormalFooter *)self.mj_footer setTitle:@"点击或上拉加载更多" forState:MJRefreshStateIdle];
  20. [(MJRefreshAutoNormalFooter *)self.mj_footer resetNoMoreData];
  21. }
  22. }];
  23. [self handleFooterRefresh:footRefreshState];
  24. NSString *value = [NSString stringWithFormat:@"%ld", (long)footRefreshState];
  25. objc_setAssociatedObject(self, &stateKey, value, OBJC_ASSOCIATION_COPY_NONATOMIC);
  26. }
  27. - (MJTableFooterRefreshState)footRefreshState {
  28. NSString *refreshState = objc_getAssociatedObject(self, &stateKey);
  29. if ([refreshState isEqualToString:@"MJTableFooterRefreshStateLoadMore"]) {
  30. return MJTableFooterRefreshStateNoMore;
  31. }
  32. else {
  33. return MJTableFooterRefreshStateLoadMore;
  34. }
  35. }
  36. - (void)handleFooterRefresh: (MJTableFooterRefreshState)footRefreshState {
  37. MJRefreshAutoNormalFooter *footer = (MJRefreshAutoNormalFooter*)self.mj_footer;
  38. switch (footRefreshState) {
  39. case MJTableFooterRefreshStateNormal:
  40. {
  41. [footer setTitle:@"" forState:MJRefreshStateIdle];
  42. break;
  43. }
  44. case MJTableFooterRefreshStateLoadMore:
  45. {
  46. [self.mj_footer endRefreshing];
  47. break;
  48. }
  49. case MJTableFooterRefreshStateNoMore:
  50. {
  51. [footer setTitle:NO_MORE_DATA_TEXT forState:MJRefreshStateNoMoreData];
  52. [self.mj_footer endRefreshingWithNoMoreData];
  53. // [(MJRefreshAutoNormalFooter *)self.mj_footer setTitle:@"到底了" forState:MJRefreshStateIdle];
  54. // [self.mj_footer endRefreshing];
  55. break;
  56. }
  57. default:
  58. break;
  59. }
  60. }
  61. @end