Skip to content

Instantly share code, notes, and snippets.

@mrdaios
Created May 29, 2015 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrdaios/87c28fe8d9dd51c835a2 to your computer and use it in GitHub Desktop.
Save mrdaios/87c28fe8d9dd51c835a2 to your computer and use it in GitHub Desktop.
通用分页配置。
//
// UIViewController+HDPaging.h
// Haidora
//
// Created by Dailingchi on 15/5/29.
// Copyright (c) 2015年 Haidora. All rights reserved.
//
#import <UIKit/UIKit.h>
/**
* 分页逻辑
*/
@interface UIViewController (HDPaging)
/**
* 当前页,默认加载HDPagingConfig中startPage.
*/
@property (nonatomic, assign) NSInteger currentPage;
/**
* 分页大小,默认加载HDPagingConfig中pageSize.
*/
@property (nonatomic, assign, readonly) NSInteger pageSize;
/**
* 分页标志,默认为NO
*/
@property (nonatomic, assign, getter=isHaveNextPage) BOOL haveNextPage;
@end
/**
* 分页配置信息
*/
@interface HDPagingConfig : NSObject
/**
* 起始页,默认为0.
*/
@property (nonatomic, assign) NSInteger startPage;
/**
* 分页大小,默认为10.
*/
@property (nonatomic, assign) NSInteger pageSize;
+ (HDPagingConfig *)sharedInstance;
@end
//
// UIViewController+Paging.m
// Haidora
//
// Created by Dailingchi on 15/5/29.
// Copyright (c) 2015年 Haidora. All rights reserved.
//
#import "UIViewController+HDPaging.h"
#import <objc/runtime.h>
#import <Aspects.h>
static char *kHDCurrentPage = "kHDCurrentPage";
static char *kHDPageSize = "kHDPageSize";
static char *kHDHaveNextPage = "kHDHaveNextPage";
@implementation UIViewController (HDPaging)
//可以移动到一个拦截器里面
// https://gitcafe.com/casatwy/iOSViewArchDemo1/blob/master/Interceptor/CTViewControllerIntercepter.m
+ (void)load
{
NSError *error;
[UIViewController aspect_hookSelector:@selector(viewDidLoad)
withOptions:AspectPositionBefore
usingBlock:^(id<AspectInfo> aspectInfo) {
//注入逻辑
UIViewController *viewController = [aspectInfo instance];
viewController.currentPage =
[HDPagingConfig sharedInstance].startPage;
viewController.pageSize = [HDPagingConfig sharedInstance].pageSize;
viewController.haveNextPage = NO;
} error:&error];
#ifdef DEBUG
if (error)
{
NSLog(@"%@", error);
}
#endif
}
#pragma mark
#pragma mark Setter/Getter
- (void)setCurrentPage:(NSInteger)currentPage
{
objc_setAssociatedObject(self, kHDCurrentPage, @(currentPage), OBJC_ASSOCIATION_ASSIGN);
}
- (NSInteger)currentPage
{
return [objc_getAssociatedObject(self, kHDCurrentPage) integerValue];
}
- (void)setPageSize:(NSInteger)pageSize
{
objc_setAssociatedObject(self, kHDPageSize, @(pageSize), OBJC_ASSOCIATION_ASSIGN);
}
- (NSInteger)pageSize
{
return [objc_getAssociatedObject(self, kHDPageSize) integerValue];
}
- (void)setHaveNextPage:(BOOL)haveNextPage
{
objc_setAssociatedObject(self, kHDHaveNextPage, @(haveNextPage), OBJC_ASSOCIATION_ASSIGN);
}
- (BOOL)isHaveNextPage
{
return [objc_getAssociatedObject(self, kHDHaveNextPage) boolValue];
}
@end
@implementation HDPagingConfig
static HDPagingConfig *sharedHDPagingConfig = nil;
+ (HDPagingConfig *)sharedInstance
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedHDPagingConfig = [[HDPagingConfig alloc] init];
sharedHDPagingConfig.startPage = 0;
sharedHDPagingConfig.pageSize = 10;
});
return sharedHDPagingConfig;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment