最近由于业务需求,需要封装这样的一个提示页面。看了网上方法感觉都大同小异,其中是很好的一个库,但是对我个人而言有点大财小用了。所以就借鉴一下其方法,自己封装一个。感觉有更高的自定义性吧。(我是初学者,请大佬爱护,勿喷) 一、封装代码:利用UIScrollView的分类实现
#importNS_ASSUME_NONNULL_BEGIN@interface UIScrollView (WD_NoData)// 需要显示的占位页面@property (nonatomic, strong) UIView *noDataView;@endNS_ASSUME_NONNULL_END复制代码
实现代码
#import "UIScrollView+WD_NoData.h"#importstatic 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 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 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;复制代码
三、效果图