TableView 无数据时展示占位视图
UITableView+NoDataView.m
#import "UITableView+NoDataView.h"
#import "NoDataView.h"
#import <objc/runtime.h> @protocol TableViewDelegate <NSObject>
@optional
- (UIView *)noDataView;
- (UIImage *)noDataViewImage;
- (NSString *)noDataViewMessage;
- (UIColor *)noDataViewMessageColor;
- (NSNumber *)noDataViewCenterYOffset; @end @implementation UITableView (NoDataView) + (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Method reloadData = class_getInstanceMethod(self, @selector(reloadData));
Method replace_reloadData = class_getInstanceMethod(self, @selector(replace_reloadData));
method_exchangeImplementations(reloadData, replace_reloadData); Method dealloc = class_getInstanceMethod(self, NSSelectorFromString(@"dealloc"));
Method replace_dealloc = class_getInstanceMethod(self, @selector(replace_dealloc));
method_exchangeImplementations(dealloc, replace_dealloc);
});
} - (void)replace_reloadData {
[self replace_reloadData]; // 忽略第一次加载
if (![self isInitFinish]) {
[self havingData:YES];
[self setIsInitFinish:YES];
return ;
} // 刷新完成之后检测数据量
dispatch_async(dispatch_get_main_queue(), ^{ NSInteger numberOfSections = [self numberOfSections];
BOOL havingData = NO;
for (NSInteger i = ; i < numberOfSections; i++) {
if ([self numberOfRowsInSection:i] > ) {
havingData = YES;
break;
}
} [self havingData:havingData];
});
} /**
展示占位图
*/
- (void)havingData:(BOOL)havingData { // 不需要显示占位图
if (havingData) {
[self freeNoDataViewIfNeeded];
self.backgroundView = nil;
return ;
} // 不需要重复创建
if (self.backgroundView) {
return ;
} // 自定义了占位图
if ([self.delegate respondsToSelector:@selector(noDataView)]) {
self.backgroundView = [self.delegate performSelector:@selector(noDataView)];
return ;
} // 使用自带的
UIImage * img = nil;
NSString * msg = @"暂无数据";
UIColor * color = [UIColor lightGrayColor];
CGFloat offset = ; // 获取图片
if ([self.delegate respondsToSelector:@selector(noDataViewImage)]) {
img = [self.delegate performSelector:@selector(noDataViewImage)];
}
// 获取文字
if ([self.delegate respondsToSelector:@selector(noDataViewMessage)]) {
msg = [self.delegate performSelector:@selector(noDataViewMessage)];
}
// 获取颜色
if ([self.delegate respondsToSelector:@selector(noDataViewMessageColor)]) {
color = [self.delegate performSelector:@selector(noDataViewMessageColor)];
}
// 获取偏移量
if ([self.delegate respondsToSelector:@selector(noDataViewCenterYOffset)]) {
offset = [[self.delegate performSelector:@selector(noDataViewCenterYOffset)] floatValue];
} // 创建占位图
self.backgroundView = [self defaultNoDataViewWithImage :img message:msg color:color offsetY:offset];
} /**
默认的占位图
*/
- (UIView *)defaultNoDataViewWithImage:(UIImage *)image message:(NSString *)message color:(UIColor *)color offsetY:(CGFloat)offset { // 计算位置, 垂直居中, 图片默认中心偏上.
CGFloat sW = self.bounds.size.width;
CGFloat cX = sW / ;
CGFloat cY = self.bounds.size.height * ( - 0.618) + offset;
CGFloat iW = image.size.width;
CGFloat iH = image.size.height; // 图片
UIImageView *imgView = [[UIImageView alloc] init];
imgView.frame = CGRectMake(cX - iW / , cY - iH / , iW, iH);
imgView.image = image; // 文字
UILabel *label = [[UILabel alloc] init];
label.font = [UIFont systemFontOfSize:];
label.textColor = color;
label.text = message;
label.textAlignment = NSTextAlignmentCenter;
label.frame = CGRectMake(, CGRectGetMaxY(imgView.frame) + , sW, label.font.lineHeight); // 视图
NoDataView *view = [[NoDataView alloc] init];
[view addSubview:imgView];
[view addSubview:label]; // 实现跟随 TableView 滚动
[view addObserver:self forKeyPath:kNoDataViewObserveKeyPath options:NSKeyValueObservingOptionNew context:nil];
return view;
} /**
监听
*/
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
if ([keyPath isEqualToString:kNoDataViewObserveKeyPath]) { /**
在 TableView 滚动 ContentOffset 改变时, 会同步改变 backgroundView 的 frame.origin.y
可以实现, backgroundView 位置相对于 TableView 不动, 但是我们希望
backgroundView 跟随 TableView 的滚动而滚动, 只能强制设置 frame.origin.y 永远为 0
兼容 MJRefresh
*/
CGRect frame = [[change objectForKey:NSKeyValueChangeNewKey] CGRectValue];
if (frame.origin.y != ) {
frame.origin.y = ;
self.backgroundView.frame = frame;
}
}
} #pragma mark - 属性 // 加载完数据的标记属性名
static NSString * const kTableViewPropertyInitFinish = @"kTableViewPropertyInitFinish"; /**
设置已经加载完成数据了
*/
- (void)setIsInitFinish:(BOOL)finish {
objc_setAssociatedObject(self, &kTableViewPropertyInitFinish, @(finish), OBJC_ASSOCIATION_ASSIGN);
} /**
是否已经加载完成数据
*/
- (BOOL)isInitFinish {
id obj = objc_getAssociatedObject(self, &kTableViewPropertyInitFinish);
return [obj boolValue];
} /**
移除 KVO 监听
*/
- (void)freeNoDataViewIfNeeded { if ([self.backgroundView isKindOfClass:[NoDataView class]]) {
[self.backgroundView removeObserver:self forKeyPath:kNoDataViewObserveKeyPath context:nil];
}
} - (void)replace_dealloc {
[self freeNoDataViewIfNeeded];
[self replace_dealloc];
NSLog(@"TableView 视图正常销毁");
} @end
UICollectionView+NoDataView.m
#import "UICollectionView+NoDataView.h"
#import <objc/runtime.h>
#import "NoDataView.h" /**
消除警告
*/
@protocol CollectionViewDelegate <NSObject>
@optional
- (UIView *)noDataView;
- (UIImage *)noDataViewImage;
- (NSString *)noDataViewMessage;
- (UIColor *)noDataViewMessageColor;
- (NSNumber *)noDataViewCenterYOffset;
@end @implementation UICollectionView (NoDataView)
/**
加载时, 交换方法
*/
+ (void)load {
// 只交换一次
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ Method reloadData = class_getInstanceMethod(self, @selector(reloadData));
Method replace_reloadData = class_getInstanceMethod(self, @selector(replace_reloadData));
method_exchangeImplementations(reloadData, replace_reloadData); Method dealloc = class_getInstanceMethod(self, NSSelectorFromString(@"dealloc"));
Method replace_dealloc = class_getInstanceMethod(self, @selector(replace_dealloc));
method_exchangeImplementations(dealloc, replace_dealloc);
});
} /**
在 ReloadData 的时候检查数据
*/
- (void)replace_reloadData { [self replace_reloadData]; // 忽略第一次加载
if (![self isInitFinish]) {
[self havingData:YES];
[self setIsInitFinish:YES];
return ;
}
// 刷新完成之后检测数据量
dispatch_async(dispatch_get_main_queue(), ^{ NSInteger numberOfSections = [self numberOfSections];
BOOL havingData = NO;
for (NSInteger i = ; i < numberOfSections; i++) {
if ([self numberOfItemsInSection:i] > ) {
havingData = YES;
break;
}
} [self havingData:havingData];
});
} /**
展示占位图
*/
- (void)havingData:(BOOL)havingData { // 不需要显示占位图
if (havingData) {
[self freeNoDataViewIfNeeded];
self.backgroundView = nil;
return ;
} // 不需要重复创建
if (self.backgroundView) {
return ;
} // 自定义了占位图
if ([self.delegate respondsToSelector:@selector(noDataView)]) {
self.backgroundView = [self.delegate performSelector:@selector(noDataView)];
return ;
} // 使用自带的
UIImage *img = nil;
NSString *msg = @"暂无数据";
UIColor *color = [UIColor lightGrayColor];
CGFloat offset = ; // 获取图片
if ([self.delegate respondsToSelector:@selector(noDataViewImage)]) {
img = [self.delegate performSelector:@selector(noDataViewImage)];
}
// 获取文字
if ([self.delegate respondsToSelector:@selector(noDataViewMessage)]) {
msg = [self.delegate performSelector:@selector(noDataViewMessage)];
}
// 获取颜色
if ([self.delegate respondsToSelector:@selector(noDataViewMessageColor)]) {
color = [self.delegate performSelector:@selector(noDataViewMessageColor)];
}
// 获取偏移量
if ([self.delegate respondsToSelector:@selector(noDataViewCenterYOffset)]) {
offset = [[self.delegate performSelector:@selector(noDataViewCenterYOffset)] floatValue];
} // 创建占位图
self.backgroundView = [self defaultNoDataViewWithImage :img message:msg color:color offsetY:offset];
} /**
默认的占位图
*/
- (UIView *)defaultNoDataViewWithImage:(UIImage *)image message:(NSString *)message color:(UIColor *)color offsetY:(CGFloat)offset { // 计算位置, 垂直居中, 图片默认中心偏上.
CGFloat sW = self.bounds.size.width;
CGFloat cX = sW / ;
CGFloat cY = self.bounds.size.height * ( - 0.618) + offset;
CGFloat iW = image.size.width;
CGFloat iH = image.size.height; // 图片
UIImageView *imgView = [[UIImageView alloc] init];
imgView.frame = CGRectMake(cX - iW / , cY - iH / , iW, iH);
imgView.image = image; // 文字
UILabel *label = [[UILabel alloc] init];
label.font = [UIFont systemFontOfSize:];
label.textColor = color;
label.text = message;
label.textAlignment = NSTextAlignmentCenter;
label.frame = CGRectMake(, CGRectGetMaxY(imgView.frame) + , sW, label.font.lineHeight); // 视图
NoDataView * view = [[NoDataView alloc] init];
[view addSubview:imgView];
[view addSubview:label]; // 实现跟随 collectionView 滚动
[view addObserver:self forKeyPath:kNoDataViewObserveKeyPath options:NSKeyValueObservingOptionNew context:nil];
return view;
} /**
监听
*/
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
if ([keyPath isEqualToString:kNoDataViewObserveKeyPath]) { /**
在 collectionView 滚动 ContentOffset 改变时, 会同步改变 backgroundView 的 frame.origin.y
可以实现, backgroundView 位置相对于 collectionView 不动, 但是我们希望
backgroundView 跟随 collectionView 的滚动而滚动, 只能强制设置 frame.origin.y 永远为 0
兼容 MJRefresh
*/
CGRect frame = [[change objectForKey:NSKeyValueChangeNewKey] CGRectValue];
if (frame.origin.y != ) {
frame.origin.y = ;
self.backgroundView.frame = frame;
}
}
} #pragma mark - 属性 /// 加载完数据的标记属性名
static NSString * const kCollectionViewPropertyInitFinish = @"kCollectionViewPropertyInitFinish"; /**
设置已经加载完成数据了
*/
- (void)setIsInitFinish:(BOOL)finish {
objc_setAssociatedObject(self, &kCollectionViewPropertyInitFinish, @(finish), OBJC_ASSOCIATION_ASSIGN);
} /**
是否已经加载完成数据
*/
- (BOOL)isInitFinish {
id obj = objc_getAssociatedObject(self, &kCollectionViewPropertyInitFinish);
return [obj boolValue];
} /**
移除 KVO 监听
*/
- (void)freeNoDataViewIfNeeded { if ([self.backgroundView isKindOfClass:[NoDataView class]]) {
[self.backgroundView removeObserver:self forKeyPath:kNoDataViewObserveKeyPath context:nil];
}
} - (void)replace_dealloc {
[self freeNoDataViewIfNeeded];
[self replace_dealloc];
NSLog(@"CollectionView 视图正常销毁");
}
@end
NoDataView.h
#import <UIKit/UIKit.h> extern NSString * const kNoDataViewObserveKeyPath; @interface NoDataView : UIView @end
NoDataView.m
#import "NoDataView.h"
NSString * const kNoDataViewObserveKeyPath = @"frame";
@implementation NoDataView - (void)dealloc {
NSLog(@"占位视图正常销毁");
} @end
调用
#import "ViewController.h"
#import "MJRefresh.h" @interface ViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView * tableView;
@property (nonatomic, strong) NSMutableArray * dataArr;
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview:self.tableView];
self.tableView.tableFooterView = [UIView new]; __weak typeof(self) weakSelf = self;
self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
[weakSelf loadData];
}];
} - (void)loadData {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.7 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.tableView.mj_header endRefreshing];
[self.tableView reloadData];
});
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return ;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
return [UITableViewCell new];
} #pragma mark - TableView 占位图 - (UIImage *)noDataViewImage {
return [UIImage imageNamed:@"note_list_no_data"];
} - (NSString *)noDataViewMessage {
return @"都用起来吧, 起飞~";
} - (UIColor *)noDataViewMessageColor {
return [UIColor blackColor];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
TableView 无数据时展示占位视图的更多相关文章
- IOS-当遇到tableView整体上移时的解决方案
方案一在使用了navigationController后,当界面进行跳转往返后,时而会出现tableView或collectionView上移的情况,通常会自动上移64个像素,那么这种情况,我们可以关 ...
- IOS开发-当遇到tableView整体上移时的解决方案
方案一在使用了navigationController后,当界面进行跳转往返后,时而会出现tableView上移的情况,通常会自动上移64个像素,那么这种情况,我们可以关闭tableView的自动适配 ...
- iOS开发之--当遇到tableView整体上移时的解决方案
方案一在使用了navigationController后,当界面进行跳转往返后,时而会出现tableView上移的情况,通常会自动上移64个像素,那么这种情况,我们可以关闭tableView的自动适配 ...
- iOS UIScrollView 滚动到当前展示的视图居中展示
需求展示: 测试效果1 first uiscrollView 宽度 为屏幕宽度 滚动步长 为 scroll 宽度的1/3 分析: 这个是最普通版 无法使每一次滚动的结果子视图居中展示, WA ...
- 看代码学知识之(2) ListView无数据时显示其他View
看代码学知识之(2) ListView无数据时显示其他View 今天看的一块布局是这样的: <!-- The frame layout is here since we will be show ...
- 父视图 使用 UIViewAnimationWithBlocks 时,如何让子视图无动画
tableView使用 UIViewAnimationWithBlocks 时 上面的cell也会一起出现动画, 所以在设置cell的时候 添加 [UIView performWithoutAnima ...
- 刷新SQL Server所有视图、函数、存储过程 更多 sql 此脚本用于在删除或添加字段时刷新相关视图,并检查视图、函数、存储过程有效性。 [SQL]代码 --视图、存储过程、函数名称 DECLARE @NAME NVARCHAR(255); --局部游标 DECLARE @CUR CURSOR --自动修改未上状态为旷课 SET @CUR=CURSOR SCROLL DYNAMIC FO
刷新SQL Server所有视图.函数.存储过程 更多 sql 此脚本用于在删除或添加字段时刷新相关视图,并检查视图.函数.存储过程有效性. [SQL]代码 --视图.存储过程.函数名称 DE ...
- [转帖]Oracle报错ORA-26563--当重命名表时碰到物化视图
Oracle报错ORA-26563--当重命名表时碰到物化视图 https://www.toutiao.com/i6739137279115133447/ 原创 波波说运维 2019-09-26 00 ...
- tableView 选中cell时,获取到当前cell
// >> 找到当前选中的cell,设置选中时的cell背景色 SideTableViewCell * cell = (SideTableViewCell *)[tableView cel ...
随机推荐
- Enrolment注册插件
自moodle2.0注册插件必须继承 lib/enrollib.php的 enrol_plugin 抽象类:这个基类包含所有标准方法和开发人员文档 课程注册信息存储在enrol和user_enrol ...
- 20165220 Java第三周学习总结
1.教材学习内容总结 类与对象 类与对象的关系:要产生对象必须先定义类,类是对象的设计图,对象是类的实例 为个别对象的数据成员指定值的方式是:对象.对象成员 注意“一类一文件” 类在定义时要使用关键词 ...
- Nowcoder contest 392 I 逛公园 (无向图割边模板)
<题目链接> 题目描述: 月月和华华一起去逛公园了.公园很大,为了方便,可以抽象的看成一个N个点M条边的无向连通图(点是景点,边是道路).公园唯一的入口在1号点,月月和华华要从这里出发,并 ...
- iframe实现伪ajax
iframe实现伪ajax 数据提交的两种方式: Form Ajax Ajax提交数据的方法: JS实现 Jquery “伪”Ajax "伪"Ajax: iframe+from实现 ...
- typescript精简版1:用ts表示常见数据类型
一:工程准备: 1.全局安装typescript npm i typescript -g // 或 yarn global add typescript vscode 配置 在命令行执行 tsc -- ...
- SQL 自定义四舍五入
--============================================== -- 自定义的四舍五入(四舍五入后的所有尾数遇进则进) -- by 小天使 2015-11-12 -- ...
- 9. Fizz Buzz 问题
Description Given number n. Print number from 1 to n. But: when number is divided by 3, print " ...
- 权限框架Apache Shiro 和 Spring Security
Shiro 首先Shiro较之 Spring Security,Shiro在保持强大功能的同时,还在简单性和灵活性方面拥有巨大优势.Shiro是一个强大而灵活的开源安全框架,能够非常清晰的处理认证.授 ...
- C#中Key事件
在键盘按下键然后释放的过程中有3个事件发生,分别为KeyDown事件.KeyPress事件和KeyUp事件. KeyDown和KeyUp事件让应用程序捕捉用户在键盘上按下的特殊键或某些特定键甚至组合键 ...
- splinter
# coding:utf-8 import tornado.web import tornado.options import tornado.httpserver import tornado.io ...