CCEmptyDataSet 0.3.3

CCEmptyDataSet 0.3.3

ash 维护。



  • ash

CCEmptyDataSet

示例

要运行示例项目,首先克隆仓库,然后从示例目录中运行 pod install

需求

安装

CCEmptyDataSet 可以通过 CocoaPods 获取。要安装它,只需将以下行添加到您的 Podfile 中

pod 'CCEmptyDataSet'

作者

ash
[email protected]

许可协议

CCEmptyDataSet 在 MIT 许可协议下提供。更多信息请参阅 LICENSE 文件。

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 "UIScrollView+EmptyDataSet.h"

除非你将其作为框架导入,否则请执行以下操作:

#import <DZNEmptyDataSet/UIScrollView+EmptyDataSet.h>

协议遵守

遵守数据源和/或代理。

@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];
}

数据源实现

返回要在空白状态下显示的内容,并利用NSAttributedString功能来自定义文本外观。

空白状态图像

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

空白状态标题的属性字符串

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

空白状态描述的属性字符串

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

用于指定按钮状态的属性字符串

- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state
{
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:17.0f]};

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

或用于指定按钮状态的图像

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

空白状态的背景颜色

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

如果您需要更复杂的布局,可以返回一个自定义视图代替

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

图像视图动画

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

此外,您还可以调整内容视图的垂直对齐方式(即:当tableHeaderView可见时很有用)

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

最后,您可以将组件相互分离(默认间隔为11点)

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