Code Monkey home page Code Monkey logo

ccemptydataset's Introduction

CCEmptyDataSet

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

Installation

CCEmptyDataSet is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'CCEmptyDataSet'

Author

ash
[email protected]

License

CCEmptyDataSet is available under the MIT license. See the LICENSE file for more info.

UIScrollView 的空视图展示

这个功能其实已经有一个很优秀的库了 DZNEmptyDataSet

我重新优化了文件结构,加入了maskView的功能

新增两个代理方法

/**
return YES to display MaskView
*/
- (BOOL)showMaskViewForEmptyDataSet:(UIScrollView * _Nullable)scrollView;

/**
 Provides 5 empty view display scenarios.
 🐟🐟🐟🐟🐟🐟🐟🐟🐟🐟🐟
 * implementation delegate other EmptyDataSetSource will be invalid.
 🐟🐟🐟🐟🐟🐟🐟🐟🐟🐟🐟
 */
- (EmptyDataSetType)showTypeForEmptyDataSet:(UIScrollView * _Nullable)scrollView;

可以通过设置EmptyDataSetType来快速设置空视图的样式,设置EmptyDataSetTypeEmptyDataSetSource的代理方法将会失效。

另外一个则是会在空视图上面默认加入一个maskView,重新获取BOOL的时机与空视图的时机一致。

使用方法

基础方法请参考DZNEmptyDataSet

快捷用法

@interface MainViewController : UITableViewController <CCEmptyDataSetSource, CCEmptyDataSetDelegate>

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
    
    // A little trick for removing the cell separators
    self.tableView.tableFooterView = [UIView new];
}

- (EmptyDataSetType)showTypeForEmptyDataSet:(UIScrollView *)scrollView {
    return random()%5;
}

使用 showTypeForEmptyDataSet 返回方法可以快速定义emptyView

@interface MainViewController : UITableViewController <CCEmptyDataSetSource, CCEmptyDataSetDelegate>
{
    CCEmptyDataSetManager *_emptyManager;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    _emptyManager = [CCEmptyDataSetManager emptyDataSetWithImage:nil title:@"11111" message:@"22222" buttonTitle:@"333333"];
    
    self.tableView.emptyDataSetSource = _emptyManager;
    self.tableView.emptyDataSetDelegate = _emptyManager;
    
    // A little trick for removing the cell separators
    self.tableView.tableFooterView = [UIView new];
}

也可以新增一个Manager类来快速定义EmptyView的属性 Manager类可以使用 Appearance 方法全局设置部分属性 具体代码可以查看demo工程中的实现

基础用法

Import

#import "UIScrollView+EmptyDataSet.h"

Unless you are importing as a framework, then do:

#import <DZNEmptyDataSet/UIScrollView+EmptyDataSet.h>

Protocol Conformance

Conform to datasource and/or delegate.

@interface MainViewController : UITableViewController <CCEmptyDataSetSource, CCEmptyDataSetDelegate>

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
    
    // A little trick for removing the cell separators
    self.tableView.tableFooterView = [UIView new];
}

Data Source Implementation

Return the content you want to show on the empty state, and take advantage of NSAttributedString features to customise the text appearance.

The image for the empty state:

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
    return [UIImage imageNamed:@"empty_placeholder"];
}

The attributed string for the title of the empty state:

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
    NSString *text = @"Please Allow Photo Access";
    
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:18.0f],
                                 NSForegroundColorAttributeName: [UIColor darkGrayColor]};
    
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}

The attributed string for the description of the empty state:

- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
{
    NSString *text = @"This allows you to share photos from your library and save photos to your camera roll.";
    
    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
{
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:17.0f]};

    return [[NSAttributedString alloc] initWithString:@"Continue" attributes:attributes];
}

or the image to be used for the specified button state:

- (UIImage *)buttonImageForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state
{
    return [UIImage imageNamed:@"button_image"];
}

The background color for the empty state:

- (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView
{
    return [UIColor whiteColor];
}

If you need a more complex layout, you can return a custom view instead:

- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView
{
    UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [activityView startAnimating];
    return activityView;
}

The image view animation

- (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;
}

Additionally, you can also adjust the vertical alignment of the content view (ie: useful when there is tableHeaderView visible):

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView
{
    return -self.tableView.tableHeaderView.frame.size.height/2.0f;
}

Finally, you can separate components from each other (default separation is 11 pts):

- (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView
{
    return 20.0f;
}

ccemptydataset's People

Contributors

aichiko0225 avatar

Stargazers

 avatar  avatar  avatar Carabineiro avatar

Watchers

James Cloos avatar Carabineiro avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.