悟空记账

UIScrollView+MJRefresh.m 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. [self willChangeValueForKey:@"mj_header"]; // KVO
  34. objc_setAssociatedObject(self, &MJRefreshHeaderKey,
  35. mj_header, OBJC_ASSOCIATION_ASSIGN);
  36. [self didChangeValueForKey:@"mj_header"]; // KVO
  37. }
  38. }
  39. - (MJRefreshHeader *)mj_header
  40. {
  41. return objc_getAssociatedObject(self, &MJRefreshHeaderKey);
  42. }
  43. #pragma mark - footer
  44. static const char MJRefreshFooterKey = '\0';
  45. - (void)setMj_footer:(MJRefreshFooter *)mj_footer
  46. {
  47. if (mj_footer != self.mj_footer) {
  48. // 删除旧的,添加新的
  49. [self.mj_footer removeFromSuperview];
  50. [self insertSubview:mj_footer atIndex:0];
  51. // 存储新的
  52. [self willChangeValueForKey:@"mj_footer"]; // KVO
  53. objc_setAssociatedObject(self, &MJRefreshFooterKey,
  54. mj_footer, OBJC_ASSOCIATION_ASSIGN);
  55. [self didChangeValueForKey:@"mj_footer"]; // KVO
  56. }
  57. }
  58. - (MJRefreshFooter *)mj_footer
  59. {
  60. return objc_getAssociatedObject(self, &MJRefreshFooterKey);
  61. }
  62. #pragma mark - 过期
  63. - (void)setFooter:(MJRefreshFooter *)footer
  64. {
  65. self.mj_footer = footer;
  66. }
  67. - (MJRefreshFooter *)footer
  68. {
  69. return self.mj_footer;
  70. }
  71. - (void)setHeader:(MJRefreshHeader *)header
  72. {
  73. self.mj_header = header;
  74. }
  75. - (MJRefreshHeader *)header
  76. {
  77. return self.mj_header;
  78. }
  79. #pragma mark - other
  80. - (NSInteger)mj_totalDataCount
  81. {
  82. NSInteger totalCount = 0;
  83. if ([self isKindOfClass:[UITableView class]]) {
  84. UITableView *tableView = (UITableView *)self;
  85. for (NSInteger section = 0; section<tableView.numberOfSections; section++) {
  86. totalCount += [tableView numberOfRowsInSection:section];
  87. }
  88. } else if ([self isKindOfClass:[UICollectionView class]]) {
  89. UICollectionView *collectionView = (UICollectionView *)self;
  90. for (NSInteger section = 0; section<collectionView.numberOfSections; section++) {
  91. totalCount += [collectionView numberOfItemsInSection:section];
  92. }
  93. }
  94. return totalCount;
  95. }
  96. static const char MJRefreshReloadDataBlockKey = '\0';
  97. - (void)setMj_reloadDataBlock:(void (^)(NSInteger))mj_reloadDataBlock
  98. {
  99. [self willChangeValueForKey:@"mj_reloadDataBlock"]; // KVO
  100. objc_setAssociatedObject(self, &MJRefreshReloadDataBlockKey, mj_reloadDataBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
  101. [self didChangeValueForKey:@"mj_reloadDataBlock"]; // KVO
  102. }
  103. - (void (^)(NSInteger))mj_reloadDataBlock
  104. {
  105. return objc_getAssociatedObject(self, &MJRefreshReloadDataBlockKey);
  106. }
  107. - (void)executeReloadDataBlock
  108. {
  109. !self.mj_reloadDataBlock ? : self.mj_reloadDataBlock(self.mj_totalDataCount);
  110. }
  111. @end
  112. @implementation UITableView (MJRefresh)
  113. + (void)load
  114. {
  115. [self exchangeInstanceMethod1:@selector(reloadData) method2:@selector(mj_reloadData)];
  116. }
  117. - (void)mj_reloadData
  118. {
  119. [self mj_reloadData];
  120. [self executeReloadDataBlock];
  121. }
  122. @end
  123. @implementation UICollectionView (MJRefresh)
  124. + (void)load
  125. {
  126. [self exchangeInstanceMethod1:@selector(reloadData) method2:@selector(mj_reloadData)];
  127. }
  128. - (void)mj_reloadData
  129. {
  130. [self mj_reloadData];
  131. [self executeReloadDataBlock];
  132. }
  133. @end