Browse Source

Enhance automaticallyHidden

Enhance automaticallyHidden
MJLee 10 years ago
parent
commit
c4941f1985

+ 1 - 1
MJRefresh.podspec

@@ -1,6 +1,6 @@
 Pod::Spec.new do |s|
 Pod::Spec.new do |s|
   s.name         = "MJRefresh"
   s.name         = "MJRefresh"
-  s.version      = "2.4.6"
+  s.version      = "2.4.7"
   s.summary      = "The easiest way to use pull-to-refresh"
   s.summary      = "The easiest way to use pull-to-refresh"
   s.homepage     = "https://github.com/CoderMJLee/MJRefresh"
   s.homepage     = "https://github.com/CoderMJLee/MJRefresh"
   s.license      = "MIT"
   s.license      = "MIT"

+ 1 - 0
MJRefresh/Base/MJRefreshComponent.h

@@ -11,6 +11,7 @@
 #import "MJRefreshConst.h"
 #import "MJRefreshConst.h"
 #import "UIView+MJExtension.h"
 #import "UIView+MJExtension.h"
 #import "UIScrollView+MJExtension.h"
 #import "UIScrollView+MJExtension.h"
+#import "UIScrollView+MJRefresh.h"
 
 
 /** 刷新控件的状态 */
 /** 刷新控件的状态 */
 typedef enum {
 typedef enum {

+ 9 - 10
MJRefresh/Base/MJRefreshFooter.m

@@ -40,21 +40,20 @@ - (void)prepare
     self.automaticallyHidden = YES;
     self.automaticallyHidden = YES;
 }
 }
 
 
-
-- (void)scrollViewContentSizeDidChange:(NSDictionary *)change
+- (void)willMoveToSuperview:(UIView *)newSuperview
 {
 {
-    [super scrollViewContentSizeDidChange:change];
+    [super willMoveToSuperview:newSuperview];
     
     
-    if (self.isAutomaticallyHidden) {
-        self.hidden = (self.scrollView.totalDataCount == 0);
+    if (newSuperview) {
+        // 监听scrollView数据的变化
+        [self.scrollView setReloadDataBlock:^(NSInteger totalDataCount) {
+            if (self.isAutomaticallyHidden) {
+                self.hidden = (self.scrollView.totalDataCount == 0);
+            }
+        }];
     }
     }
 }
 }
 
 
-- (void)setAutomaticallyHidden:(BOOL)automaticallyHidden
-{
-    _automaticallyHidden = automaticallyHidden;
-}
-
 #pragma mark - 公共方法
 #pragma mark - 公共方法
 - (void)noticeNoMoreData
 - (void)noticeNoMoreData
 {
 {

+ 0 - 2
MJRefresh/UIScrollView+MJExtension.h

@@ -20,6 +20,4 @@
 
 
 @property (assign, nonatomic) CGFloat mj_contentW;
 @property (assign, nonatomic) CGFloat mj_contentW;
 @property (assign, nonatomic) CGFloat mj_contentH;
 @property (assign, nonatomic) CGFloat mj_contentH;
-
-- (NSInteger)totalDataCount;
 @end
 @end

+ 0 - 19
MJRefresh/UIScrollView+MJExtension.m

@@ -107,23 +107,4 @@ - (CGFloat)mj_contentH
 {
 {
     return self.contentSize.height;
     return self.contentSize.height;
 }
 }
-
-- (NSInteger)totalDataCount
-{
-    NSInteger totalCount = 0;
-    if ([self isKindOfClass:[UITableView class]]) {
-        UITableView *tableView = (UITableView *)self;
-        
-        for (NSInteger section = 0; section<tableView.numberOfSections; section++) {
-            totalCount += [tableView numberOfRowsInSection:section];
-        }
-    } else if ([self isKindOfClass:[UICollectionView class]]) {
-        UICollectionView *collectionView = (UICollectionView *)self;
-        
-        for (NSInteger section = 0; section<collectionView.numberOfSections; section++) {
-            totalCount += [collectionView numberOfItemsInSection:section];
-        }
-    }
-    return totalCount;
-}
 @end
 @end

+ 4 - 0
MJRefresh/UIScrollView+MJRefresh.h

@@ -16,4 +16,8 @@
 @property (strong, nonatomic) MJRefreshHeader *header;
 @property (strong, nonatomic) MJRefreshHeader *header;
 /** 上拉刷新控件 */
 /** 上拉刷新控件 */
 @property (strong, nonatomic) MJRefreshFooter *footer;
 @property (strong, nonatomic) MJRefreshFooter *footer;
+
+#pragma mark - other
+- (NSInteger)totalDataCount;
+@property (copy, nonatomic) void (^reloadDataBlock)(NSInteger totalDataCount);
 @end
 @end

+ 179 - 0
MJRefresh/UIScrollView+MJRefresh.m

@@ -12,6 +12,20 @@
 #import "MJRefreshFooter.h"
 #import "MJRefreshFooter.h"
 #import <objc/runtime.h>
 #import <objc/runtime.h>
 
 
+@implementation NSObject (MJRefresh)
+
++ (void)exchangeInstanceMethod1:(SEL)method1 method2:(SEL)method2
+{
+    method_exchangeImplementations(class_getInstanceMethod(self, method1), class_getInstanceMethod(self, method2));
+}
+
++ (void)exchangeClassMethod1:(SEL)method1 method2:(SEL)method2
+{
+    method_exchangeImplementations(class_getClassMethod(self, method1), class_getClassMethod(self, method2));
+}
+
+@end
+
 @implementation UIScrollView (MJRefresh)
 @implementation UIScrollView (MJRefresh)
 
 
 #pragma mark - header
 #pragma mark - header
@@ -57,4 +71,169 @@ - (MJRefreshFooter *)footer
 {
 {
     return objc_getAssociatedObject(self, &MJRefreshFooterKey);
     return objc_getAssociatedObject(self, &MJRefreshFooterKey);
 }
 }
+
+#pragma mark - other
+- (NSInteger)totalDataCount
+{
+    NSInteger totalCount = 0;
+    if ([self isKindOfClass:[UITableView class]]) {
+        UITableView *tableView = (UITableView *)self;
+        
+        for (NSInteger section = 0; section<tableView.numberOfSections; section++) {
+            totalCount += [tableView numberOfRowsInSection:section];
+        }
+    } else if ([self isKindOfClass:[UICollectionView class]]) {
+        UICollectionView *collectionView = (UICollectionView *)self;
+        
+        for (NSInteger section = 0; section<collectionView.numberOfSections; section++) {
+            totalCount += [collectionView numberOfItemsInSection:section];
+        }
+    }
+    return totalCount;
+}
+
+static const char MJRefreshReloadDataBlockKey = '\0';
+- (void)setReloadDataBlock:(void (^)(NSInteger))reloadDataBlock
+{
+    [self willChangeValueForKey:@"reloadDataBlock"]; // KVO
+    objc_setAssociatedObject(self, &MJRefreshReloadDataBlockKey, reloadDataBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
+    [self willChangeValueForKey:@"reloadDataBlock"]; // KVO
+}
+
+- (void (^)(NSInteger))reloadDataBlock
+{
+    return objc_getAssociatedObject(self, &MJRefreshReloadDataBlockKey);
+}
+
+- (void)executeReloadDataBlock
+{
+    void (^reloadDataBlock)(NSInteger) = self.reloadDataBlock;
+    !reloadDataBlock ? : reloadDataBlock(self.totalDataCount);
+}
+@end
+
+@implementation UITableView (MJRefresh)
+
++ (void)load
+{
+    [self exchangeInstanceMethod1:@selector(reloadData) method2:@selector(mj_reloadData)];
+    [self exchangeInstanceMethod1:@selector(reloadRowsAtIndexPaths:withRowAnimation:) method2:@selector(mj_reloadRowsAtIndexPaths:withRowAnimation:)];
+    [self exchangeInstanceMethod1:@selector(deleteRowsAtIndexPaths:withRowAnimation:) method2:@selector(mj_deleteRowsAtIndexPaths:withRowAnimation:)];
+    [self exchangeInstanceMethod1:@selector(insertRowsAtIndexPaths:withRowAnimation:) method2:@selector(mj_insertRowsAtIndexPaths:withRowAnimation:)];
+    [self exchangeInstanceMethod1:@selector(reloadSections:withRowAnimation:) method2:@selector(mj_reloadSections:withRowAnimation:)];
+    [self exchangeInstanceMethod1:@selector(deleteSections:withRowAnimation:) method2:@selector(mj_deleteSections:withRowAnimation:)];
+    [self exchangeInstanceMethod1:@selector(insertSections:withRowAnimation:) method2:@selector(mj_insertSections:withRowAnimation:)];
+}
+
+- (void)mj_reloadData
+{
+    [self mj_reloadData];
+    
+    [self executeReloadDataBlock];
+}
+
+- (void)mj_insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
+{
+    [self mj_insertRowsAtIndexPaths:indexPaths withRowAnimation:animation];
+    
+    [self executeReloadDataBlock];
+}
+
+- (void)mj_deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
+{
+    [self mj_deleteRowsAtIndexPaths:indexPaths withRowAnimation:animation];
+    
+    [self executeReloadDataBlock];
+}
+
+- (void)mj_reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
+{
+    [self mj_reloadRowsAtIndexPaths:indexPaths withRowAnimation:animation];
+    
+    [self executeReloadDataBlock];
+}
+
+- (void)mj_insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation
+{
+    [self mj_insertSections:sections withRowAnimation:animation];
+    
+    [self executeReloadDataBlock];
+}
+
+- (void)mj_deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation
+{
+    [self mj_deleteSections:sections withRowAnimation:animation];
+    
+    [self executeReloadDataBlock];
+}
+
+- (void)mj_reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation
+{
+    [self mj_reloadSections:sections withRowAnimation:animation];
+    
+    [self executeReloadDataBlock];
+}
+@end
+
+@implementation UICollectionView (MJRefresh)
+
++ (void)load
+{
+    [self exchangeInstanceMethod1:@selector(reloadData) method2:@selector(mj_reloadData)];
+    [self exchangeInstanceMethod1:@selector(reloadItemsAtIndexPaths:) method2:@selector(mj_reloadItemsAtIndexPaths:)];
+    [self exchangeInstanceMethod1:@selector(insertItemsAtIndexPaths:) method2:@selector(mj_insertItemsAtIndexPaths:)];
+    [self exchangeInstanceMethod1:@selector(deleteItemsAtIndexPaths:) method2:@selector(mj_deleteItemsAtIndexPaths:)];
+    [self exchangeInstanceMethod1:@selector(reloadSections:) method2:@selector(mj_reloadSections:)];
+    [self exchangeInstanceMethod1:@selector(insertSections:) method2:@selector(mj_insertSections:)];
+    [self exchangeInstanceMethod1:@selector(deleteSections:) method2:@selector(mj_deleteSections:)];
+}
+
+- (void)mj_reloadData
+{
+    [self mj_reloadData];
+    
+    [self executeReloadDataBlock];
+}
+
+- (void)mj_insertSections:(NSIndexSet *)sections
+{
+    [self mj_insertSections:sections];
+    
+    [self executeReloadDataBlock];
+}
+
+- (void)mj_deleteSections:(NSIndexSet *)sections
+{
+    [self mj_deleteSections:sections];
+    
+    [self executeReloadDataBlock];
+}
+
+- (void)mj_reloadSections:(NSIndexSet *)sections
+{
+    [self mj_reloadSections:sections];
+    
+    [self executeReloadDataBlock];
+}
+
+- (void)mj_insertItemsAtIndexPaths:(NSArray *)indexPaths
+{
+    [self mj_insertItemsAtIndexPaths:indexPaths];
+    
+    [self executeReloadDataBlock];
+}
+
+- (void)mj_deleteItemsAtIndexPaths:(NSArray *)indexPaths
+{
+    [self mj_deleteItemsAtIndexPaths:indexPaths];
+    
+    [self executeReloadDataBlock];
+}
+
+- (void)mj_reloadItemsAtIndexPaths:(NSArray *)indexPaths
+{
+    [self mj_reloadItemsAtIndexPaths:indexPaths];
+    
+    [self executeReloadDataBlock];
+}
 @end
 @end

BIN
MJRefreshExample/MJRefreshExample.xcodeproj/project.xcworkspace/xcuserdata/mj.xcuserdatad/UserInterfaceState.xcuserstate