有時候項目中有這樣的需求,頁面包含一個UITableVeiw,當沒有數據的時候顯示一張占位圖片,點擊圖片可以重新刷新數據。效果圖如下:
有數據時.png
沒數據時.png
思路
為UITableView增加個Category,利用方法method_exchangeImplementations交換reloadData方法為real_ReloadData,在real_ReloadData方法內判斷是否有數據源,再動態顯示占位的UIView。
在創建UITableView的地方一句代碼設置占位控件。
tableView.placeHolderView = [[PlaceholderView alloc]initWithFrame:tableView.bounds onTapView:^{
//點擊占位圖片
}];
[self.view addSubview:tableView];
UITableView+Placeholder.h文件
#import <UIKit/UIKit.h>
@interface UITableView (Placeholder)
/* 占位圖 */
@property (nonatomic, strong) UIView *placeHolderView;
@end
UITableView+Placeholder.m文件
#import "UITableView+Placeholder.h"
#import <objc/runtime.h>
@implementation UITableView (Placeholder)
+ (void)load
{
//交換刷新方法
Method reloadData =class_getInstanceMethod(self, @selector(reloadData));
Method real_ReloadData =class_getInstanceMethod(self, @selector(real_ReloadData));
method_exchangeImplementations(reloadData, real_ReloadData);
}
-(void)real_ReloadData
{
[self dataIsEmpty];
[self real_ReloadData];
}
/** 判斷是否有數據源,沒有數據源則顯示占位圖片 */
- (void)dataIsEmpty
{
BOOL isEmpty = YES;
id<UITableViewDataSource> dataSourse = self.dataSource;
//檢測是否有數據源
if ([dataSourse respondsToSelector:@selector(numberOfSectionsInTableView:)]) {//group
NSInteger sections = [dataSourse numberOfSectionsInTableView:self];
if (sections >0) {
isEmpty = NO;
}
}else if([dataSourse respondsToSelector:@selector(tableView:numberOfRowsInSection:)]){//plain
NSInteger rows = [dataSourse tableView:self numberOfRowsInSection:0];
if (rows >0) {
isEmpty = NO;
}
}
if (isEmpty) {//顯示占位圖
[self.placeHolderView removeFromSuperview];
[self addSubview:self.placeHolderView];
}else{//移除占位圖片
[self.placeHolderView removeFromSuperview];
}
}
- (void)setPlaceHolderView:(UIView *)placeHolderView
{
objc_setAssociatedObject(self, @selector(placeHolderView), placeHolderView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIView *)placeHolderView
{
return objc_getAssociatedObject(self, @selector(placeHolderView));
}
自定義占位的UIView 為PlaceholderView
PlaceholderView.h文件
#import <UIKit/UIKit.h>
//點擊后的回調
typedef void(^TapBlock)();
@interface PlaceholderView : UIView
@property(nonatomic,copy)TapBlock onTapBlock;
- (instancetype)initWithFrame:(CGRect)frame onTapView:(TapBlock)onTapBlock;
@end
PlaceholderView.m文件
#import "PlaceholderView.h"
@implementation PlaceholderView
{
UIImageView *_imageView;
}
- (instancetype)initWithFrame:(CGRect)frame onTapView:(TapBlock)onTapBlock
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor whiteColor];
self.onTapBlock = onTapBlock;
[self setupSubViews];
}
return self;
}
-(void)setupSubViews
{
UIImageView *imageView =[[UIImageView alloc]init];
imageView.userInteractionEnabled = YES;
imageView.contentMode = UIViewContentModeCenter;
[imageView setImage:[UIImage imageNamed:@"nodataPlaceholder"]];
_imageView = imageView;
[self addSubview:imageView];
[self addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onTapView:)]];
}
-(void)layoutSubviews
{
[super layoutSubviews];
_imageView.frame = self.bounds;
}
/** 增加點擊手勢 **/
-(void)onTapView:(UITapGestureRecognizer *)recognizer
{
if (self.onTapBlock) {
self.onTapBlock();
}
}
這樣當這個需求發生變化時,改動相對來說更小。當然,還可以把設置placeholderView的代碼也封裝到UItableView分類的init里面去。
點擊下載Demo 有什么錯誤歡迎批評指正 。