数据加载为空占位图DZNEmptyDataSet使用
2016-12-08 本文已影响2858人
我想走走
先来两张图片压压惊。。。。
2C585AF22652AD42B9D20135E656CB06.jpg 617927F5D6166D63B6505B321341F8D5.jpg好了,不要沉浸在美景中,重点来了。
我用的是第三方DZNEmptyDataSet,非常方便。非常实用。先看一下效果图。O(∩∩)O
不要问我蓉蓉是谁。。。 ^^。
首先搜索第三方,导入,我中共有两个界面
一个是ViewController,还有一个是NewTableViewController
这两个界面用到的方法是不一样的。
一个是创建View,另一个是创建按钮。仔细看一下就知道咯。亲。
//
// ViewController.m
// DZNEmptyDataSetUserForMe
//
// Created by mibo02 on 16/12/8.
// Copyright © 2016年 mibo02. All rights reserved.
//
#import "ViewController.h"
#import "UIScrollView+EmptyDataSet.h"
#import "NewTableViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>
@property (nonatomic, strong)UITableView *tableview;
@property (nonatomic, getter=isLoading)BOOL loading;
@end
@implementation ViewController
- (void)setLoading:(BOOL)loading
{
if (self.loading == loading) {
return;
}
_loading = loading;
[self.tableview reloadEmptyDataSet];
}
- (IBAction)NextAction:(UIBarButtonItem *)sender {
NewTableViewController *new = [NewTableViewController new];
[self.navigationController pushViewController:new animated:YES];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height) style:(UITableViewStylePlain)];
self.tableview.delegate =self;
self.tableview.dataSource = self;
self.tableview.emptyDataSetSource = self;
self.tableview.emptyDataSetDelegate = self;
[self.view addSubview:self.tableview];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *str = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:str];
}
return cell;
}
//上标题(返回标题)
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
NSString *text = @"峰峰 和 蓉蓉";
UIFont *font = [UIFont systemFontOfSize:17];
UIColor *color = [UIColor redColor];
NSMutableDictionary *attribult = [NSMutableDictionary new];
[attribult setObject:font forKey:NSFontAttributeName];
[attribult setObject:color forKey:NSForegroundColorAttributeName];
return [[NSAttributedString alloc] initWithString:text attributes:attribult];
}
//详情标题(返回详情标题)
- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
{
NSString *text = @"点击重新加载";
NSMutableDictionary *attributes = [NSMutableDictionary new];
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.lineBreakMode = NSLineBreakByWordWrapping;
paragraph.alignment = NSTextAlignmentCenter;
[attributes setObject:[UIFont systemFontOfSize:17] forKey:NSFontAttributeName];
[attributes setObject:[UIColor greenColor] forKey:NSForegroundColorAttributeName];
[attributes setValue:paragraph forKey:NSParagraphStyleAttributeName];
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:text attributes:attributes];
return attributeString;
}
//返回单张图片
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
if (self.isLoading) {
return [UIImage imageNamed:@"loading_imgBlue_78x78"];
} else {
return [UIImage imageNamed:@"placeholder_kickstarter"];
}
}
//让图片进行旋转
- (CAAnimation *)imageAnimationForEmptyDataSet:(UIScrollView *)scrollView
{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
animation.toValue = [NSValue valueWithCATransform3D: CATransform3DMakeRotation(M_PI_2, 0.0, 0.0, 1.0) ];
animation.duration = 0.25;
animation.cumulative = YES;
animation.repeatCount = MAXFLOAT;
return animation;
}
#pragma mark - DZNEmptyDataSetDelegate Methods
- (BOOL)emptyDataSetShouldAnimateImageView:(UIScrollView *)scrollView
{
return self.isLoading;
}
//点击view加载三秒后停止
- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view
{
self.loading = YES;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.loading = NO;
});
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
//
// NewTableViewController.m
// DZNEmptyDataSetUserForMe
//
// Created by mibo02 on 16/12/8.
// Copyright © 2016年 mibo02. All rights reserved.
//
#import "NewTableViewController.h"
#import "UIScrollView+EmptyDataSet.h"
@interface NewTableViewController ()<DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>
@property (nonatomic, getter=isLoading)BOOL loading;
@end
@implementation NewTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.emptyDataSetSource = self;
self.tableView.emptyDataSetDelegate = self;
}
- (void)setLoading:(BOOL)loading
{
if (self.loading == loading) {
return;
}
_loading = loading;
[self.tableView reloadEmptyDataSet];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *str = @"reuseIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:str];
}
return cell;
}
#pragma mark - DZNEmptyDataSetSource Methods
//按钮
- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state
{
NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
[dic setValue:[UIColor redColor] forKey:NSForegroundColorAttributeName];
[dic setValue:[UIFont systemFontOfSize:20] forKey:NSFontAttributeName];
return [[NSAttributedString alloc] initWithString:@"点击重新加载..." attributes:dic];
}
//设置Button 的 图片
//- (UIImage *)buttonBackgroundImageForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state
//{
// return [UIImage imageNamed:@"placeholder_kickstarter"];
//}
//背景颜色(当没有数据的时候设置背景颜色)
- (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView
{
return [UIColor lightGrayColor];
}
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
if (self.isLoading) {
return [UIImage imageNamed:@"loading_imgBlue_78x78"];
}
else {
return [UIImage imageNamed:@"placeholder_kickstarter"];
}
}
- (CAAnimation *)imageAnimationForEmptyDataSet:(UIScrollView *)scrollView
{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
animation.toValue = [NSValue valueWithCATransform3D: CATransform3DMakeRotation(M_PI_2, 0.0, 0.0, 1.0) ];
animation.duration = 0.25;
animation.cumulative = YES;
animation.repeatCount = MAXFLOAT;
return animation;
}
//返回loading的状态
- (BOOL)emptyDataSetShouldAnimateImageView:(UIScrollView *)scrollView
{
return self.isLoading;
}
- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button
{
self.loading = YES;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.loading = NO;
});
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
DEMO在这里 https://github.com/fengfengaima/DZNEmptyDemo.git
如果对你有帮助,别忘了给个赞哦。