最近由於業務需求,需要封裝這樣的一個提示頁面。看了網上方法感覺都大同小異,其中DZNEmptyDataSet是很好的一個庫,但是對我個人而言有點大財小用了。所以就借鑑一下其方法,自己封裝一個。感覺有更高的自定義性吧。(我是初學者,請大佬愛護,勿噴) 一、封裝程式碼:利用UIScrollView的分類實現
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UIScrollView (WD_NoData)
// 需要顯示的佔位頁面
@property (nonatomic, strong) UIView *noDataView;
@end
NS_ASSUME_NONNULL_END
複製程式碼
實現程式碼
#import "UIScrollView+WD_NoData.h"
#import <objc/runtime.h>
static char *noDataViewKey = "noDataViewKey";
@implementation UIScrollView (WD_NoData)
/**
交換方法
@param sel1 原方法
@param sel2 自定義方法
@param cls 類
*/
void exchangeSelector(SEL sel1, SEL sel2, Class cls) {
Class class = [cls class];
Method originalMethod = class_getInstanceMethod(class, sel1);
Method swizzledMethod = class_getInstanceMethod(class, sel2);
BOOL success = class_addMethod(class, sel1, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
if (success) {
class_replaceMethod(class, sel2, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
#pragma mark =============== Setter ===============
- (void)setNoDataView:(UIView *)noDataView {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
exchangeSelector(@selector(reloadData), @selector(wd_reloadData), [self class]);
});
objc_setAssociatedObject(self, noDataViewKey, noDataView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
#pragma mark =============== Getter ===============
- (UIView *)noDataView {
UIView *noDataView = objc_getAssociatedObject(self, noDataViewKey);
noDataView.frame = self.frame;
return noDataView;
}
- (void)wd_reloadData {
[self wd_reloadData];
[self wd_checkData];
}
#pragma mark =============== 獲取資料 ===============
- (void)wd_checkData {
NSInteger items = 0;
if (![self respondsToSelector:@selector(dataSource)]) {
return;
}
// UITableView support
if ([self isKindOfClass:[UITableView class]]) {
UITableView *tableView = (UITableView *)self;
id <UITableViewDataSource> dataSource = tableView.dataSource;
NSInteger sections = 1;
if (dataSource && [dataSource respondsToSelector:@selector(numberOfSectionsInTableView:)]) {
sections = [dataSource numberOfSectionsInTableView:tableView];
}
if (dataSource && [dataSource respondsToSelector:@selector(tableView:numberOfRowsInSection:)]) {
for (NSInteger section = 0; section < sections; section++) {
items += [dataSource tableView:tableView numberOfRowsInSection:section];
}
}
}
// UICollectionView support
else if ([self isKindOfClass:[UICollectionView class]]) {
UICollectionView *collectionView = (UICollectionView *)self;
id <UICollectionViewDataSource> dataSource = collectionView.dataSource;
NSInteger sections = 1;
if (dataSource && [dataSource respondsToSelector:@selector(numberOfSectionsInCollectionView:)]) {
sections = [dataSource numberOfSectionsInCollectionView:collectionView];
}
if (dataSource && [dataSource respondsToSelector:@selector(collectionView:numberOfItemsInSection:)]) {
for (NSInteger section = 0; section < sections; section++) {
items += [dataSource collectionView:collectionView numberOfItemsInSection:section];
}
}
}
if ( items == 0 ) {
[self.superview addSubview:self.noDataView];
} else {
[self.noDataView removeFromSuperview];
}
}
@end
複製程式碼
二、使用方法
#import "UIScrollView+WD_NoData.h"
// 自定義頁面
#import "WDTestEmptyView.h"
複製程式碼
//示例的檢視
WDTestEmptyView *view = [NSBundle.mainBundle loadNibNamed:@"WDTestEmptyView" owner:self options:nil].firstObject;
// 設定檢視
self.tableView.noDataView = view;
複製程式碼
三、效果圖