第三方库DZNEmptyDataSet使用示例

来源:互联网 发布:豪迪群发软件 编辑:程序博客网 时间:2024/06/18 16:43

本文介绍怎么使用第三方库DZNEmptyDataSet,以后项目开发时可能会用到。

程序执行后的界面为:


下面上干货。

1,向工程中加入第三方库。加完后的工程应该是这样的:


2.在故事板中增加一个tableView控件,设置好四个方向的约束属性。



3.设置outlet绑定、协议委托,具体.m和.h代码为:

////  ViewController.h//  TestDZNEmptyDataSet//#import <UIKit/UIKit.h>/* STEP1:引入第三方类库 */#import "UIScrollView+EmptyDataSet.h"/* STEP2:引入代理协议 */@interface ViewController : UIViewController <DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>/* STEP3:拖动一个TableView控件并进行关联 */@property (strong, nonatomic) IBOutlet UITableView *theTableView;@end
////  ViewController.m//  TestDZNEmptyDataSet#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {        /* STEP4:设置代理 */    self.theTableView.emptyDataSetSource = self;    self.theTableView.emptyDataSetDelegate = self;        /* STEP5:去掉TableView中的默认横线 */    self.theTableView.tableFooterView = [UIView new];        [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}/* STEP6: Return the content you want to show on the empty state, and take advantage of NSAttributedString  features to customise the text appearance.*/#pragma mark ---- Data Source Implementation ----/* The image for the empty state */- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView{    NSLog(@"imageForEmptyDataSet:empty_placeholder");    return [UIImage imageNamed:@"empty_placeholder"];}/* The image view animation */- (CAAnimation *)imageAnimationForEmptyDataSet:(UIScrollView *)scrollView{    NSLog(@"imageAnimationForEmptyDataSet");        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;}#if 0/* The attributed string for the title of the empty state */- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView{    NSLog(@"titleForEmptyDataSet:Please Allow Photo Access");        NSString *text = @"Please Allow Photo Access";        NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:18.0f],                                 NSForegroundColorAttributeName: [UIColor darkGrayColor]};        return [[NSAttributedString alloc] initWithString:text attributes:attributes];}#endif/* The attributed string for the description of the empty state */- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView{    NSLog(@"descriptionForEmptyDataSet:您还没有上传照片");        NSString *text = @"您还没有上传照片";        NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];    paragraph.lineBreakMode = NSLineBreakByWordWrapping;    paragraph.alignment = NSTextAlignmentCenter;        NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0f],                                 NSForegroundColorAttributeName: [UIColor lightGrayColor],                                 NSParagraphStyleAttributeName: paragraph};    return [[NSAttributedString alloc] initWithString:text attributes:attributes];}/* The attributed string to be used for the specified button state */- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state{    NSLog(@"buttonTitleForEmptyDataSet:点击上传照片");        NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:17.0f]};    return [[NSAttributedString alloc] initWithString:@"点击上传照片" attributes:attributes];}#if 0/* or the image to be used for the specified button state. 注:图片优先级高于文字. */- (UIImage *)buttonImageForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state{    NSLog(@"buttonImageForEmptyDataSet:button_image");    return [UIImage imageNamed:@"button_image"];}#endif/* The background color for the empty state */- (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView{    NSLog(@"backgroundColorForEmptyDataSet");    return [UIColor brownColor]; // redColor whiteColor}#if 0/* 显示等待状态: If you need a more complex layout, you can return a custom view instead: */- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView{    NSLog(@"customViewForEmptyDataSet");        UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];    [activityView startAnimating];    return activityView;}#endif/* Additionally, you can also adjust the vertical alignment of the content view  (ie: useful when there is tableHeaderView visible): */- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView{    NSLog(@"verticalOffsetForEmptyDataSet");    return -self.theTableView.tableHeaderView.frame.size.height/2.0f;}/* Finally, you can separate components from each other (default separation is 11 pts): */- (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView{    NSLog(@"spaceHeightForEmptyDataSet");    return 20.0f;}/* STEP7: Return the behaviours you would expect from the empty states, and receive the user events.*/#pragma mark ---- Delegate Implementation ----/* Asks to know if the empty state should be rendered and displayed (Default is YES) : */- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView{    NSLog(@"emptyDataSetShouldDisplay");    return YES;}/* Asks for interaction permission (Default is YES) : */- (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView{    NSLog(@"emptyDataSetShouldAllowTouch");    return YES;}/* Asks for scrolling permission (Default is NO) : */- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView{    NSLog(@"emptyDataSetShouldAllowScroll");    return NO;}/* Asks for image view animation permission (Default is NO) : */- (BOOL) emptyDataSetShouldAllowImageViewAnimate:(UIScrollView *)scrollView{    NSLog(@"emptyDataSetShouldAllowImageViewAnimate");    return YES;}/* Notifies when the dataset view was tapped: */- (void)emptyDataSetDidTapView:(UIScrollView *)scrollView{    NSLog(@"emptyDataSetDidTapView");    // Do something}/* Notifies when the data set call to action button was tapped: */- (void)emptyDataSetDidTapButton:(UIScrollView *)scrollView{    NSLog(@"emptyDataSetDidTapButton");    // Do something}@end

4.代码中使用到了图片文件,因此增加相应的image文件:



5.代码里还有一些是if 0注掉的代码,这些代码在实际使用中可以根据需要放开。

好了,到这里就完成了,很简单吧。

2 0
原创粉丝点击