UIScrollView+MJRefresh.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // 代码地址: https://github.com/CoderMJLee/MJRefresh
  2. // 代码地址: http://code4app.com/ios/%E5%BF%AB%E9%80%9F%E9%9B%86%E6%88%90%E4%B8%8B%E6%8B%89%E4%B8%8A%E6%8B%89%E5%88%B7%E6%96%B0/52326ce26803fabc46000000
  3. // UIScrollView+MJRefresh.m
  4. // MJRefreshExample
  5. //
  6. // Created by MJ Lee on 15/3/4.
  7. // Copyright (c) 2015年 小码哥. All rights reserved.
  8. //
  9. #import "UIScrollView+MJRefresh.h"
  10. #import "MJRefreshHeader.h"
  11. #import "MJRefreshFooter.h"
  12. #import <objc/runtime.h>
  13. @implementation NSObject (MJRefresh)
  14. + (void)exchangeInstanceMethod1:(SEL)method1 method2:(SEL)method2
  15. {
  16. method_exchangeImplementations(class_getInstanceMethod(self, method1), class_getInstanceMethod(self, method2));
  17. }
  18. + (void)exchangeClassMethod1:(SEL)method1 method2:(SEL)method2
  19. {
  20. method_exchangeImplementations(class_getClassMethod(self, method1), class_getClassMethod(self, method2));
  21. }
  22. @end
  23. @implementation UIScrollView (MJRefresh)
  24. #pragma mark - header
  25. static const char MJRefreshHeaderKey = '\0';
  26. - (void)setMj_header:(MJRefreshHeader *)mj_header
  27. {
  28. if (mj_header != self.mj_header) {
  29. // 删除旧的,添加新的
  30. [self.mj_header removeFromSuperview];
  31. [self insertSubview:mj_header atIndex:0];
  32. // 存储新的
  33. objc_setAssociatedObject(self, &MJRefreshHeaderKey,
  34. mj_header, OBJC_ASSOCIATION_RETAIN);
  35. }
  36. }
  37. - (MJRefreshHeader *)mj_header
  38. {
  39. return objc_getAssociatedObject(self, &MJRefreshHeaderKey);
  40. }
  41. #pragma mark - footer
  42. static const char MJRefreshFooterKey = '\0';
  43. - (void)setMj_footer:(MJRefreshFooter *)mj_footer
  44. {
  45. if (mj_footer != self.mj_footer) {
  46. // 删除旧的,添加新的
  47. [self.mj_footer removeFromSuperview];
  48. [self insertSubview:mj_footer atIndex:0];
  49. // 存储新的
  50. objc_setAssociatedObject(self, &MJRefreshFooterKey,
  51. mj_footer, OBJC_ASSOCIATION_RETAIN);
  52. }
  53. }
  54. - (MJRefreshFooter *)mj_footer
  55. {
  56. return objc_getAssociatedObject(self, &MJRefreshFooterKey);
  57. }
  58. #pragma mark - 过期
  59. - (void)setFooter:(MJRefreshFooter *)footer
  60. {
  61. self.mj_footer = footer;
  62. }
  63. - (MJRefreshFooter *)footer
  64. {
  65. return self.mj_footer;
  66. }
  67. - (void)setHeader:(MJRefreshHeader *)header
  68. {
  69. self.mj_header = header;
  70. }
  71. - (MJRefreshHeader *)header
  72. {
  73. return self.mj_header;
  74. }
  75. #pragma mark - other
  76. - (NSInteger)mj_totalDataCount
  77. {
  78. NSInteger totalCount = 0;
  79. if ([self isKindOfClass:[UITableView class]]) {
  80. UITableView *tableView = (UITableView *)self;
  81. for (NSInteger section = 0; section<tableView.numberOfSections; section++) {
  82. totalCount += [tableView numberOfRowsInSection:section];
  83. }
  84. } else if ([self isKindOfClass:[UICollectionView class]]) {
  85. UICollectionView *collectionView = (UICollectionView *)self;
  86. for (NSInteger section = 0; section<collectionView.numberOfSections; section++) {
  87. totalCount += [collectionView numberOfItemsInSection:section];
  88. }
  89. }
  90. return totalCount;
  91. }
  92. static const char MJRefreshReloadDataBlockKey = '\0';
  93. - (void)setMj_reloadDataBlock:(void (^)(NSInteger))mj_reloadDataBlock
  94. {
  95. [self willChangeValueForKey:@"mj_reloadDataBlock"]; // KVO
  96. objc_setAssociatedObject(self, &MJRefreshReloadDataBlockKey, mj_reloadDataBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
  97. [self didChangeValueForKey:@"mj_reloadDataBlock"]; // KVO
  98. }
  99. - (void (^)(NSInteger))mj_reloadDataBlock
  100. {
  101. return objc_getAssociatedObject(self, &MJRefreshReloadDataBlockKey);
  102. }
  103. - (void)executeReloadDataBlock
  104. {
  105. !self.mj_reloadDataBlock ? : self.mj_reloadDataBlock(self.mj_totalDataCount);
  106. }
  107. @end
  108. @implementation UITableView (MJRefresh)
  109. + (void)load
  110. {
  111. [self exchangeInstanceMethod1:@selector(reloadData) method2:@selector(mj_reloadData)];
  112. }
  113. - (void)mj_reloadData
  114. {
  115. [self mj_reloadData];
  116. [self executeReloadDataBlock];
  117. }
  118. @end
  119. @implementation UICollectionView (MJRefresh)
  120. + (void)load
  121. {
  122. [self exchangeInstanceMethod1:@selector(reloadData) method2:@selector(mj_reloadData)];
  123. }
  124. - (void)mj_reloadData
  125. {
  126. [self mj_reloadData];
  127. [self executeReloadDataBlock];
  128. }
  129. @end