EmptySet:iOS列表占位处理

作者: Charles___ | 来源:发表于2017-04-21 11:47 被阅读295次

描述:

列表没有数据、无网络状态下显示占位图处理

场景:

列表没有数据、无网络状态

原理:

捕捉UITableView/UICollectionView的reloadData事件,判断列表有无数据,处理展示占位图逻辑

介绍:
  1. EmpySet使用分类UIScrollView+CYEmptySet实现
  2. 对无任何侵入性,不依赖于任何三方库
使用:
Paste_Image.png
Demo展示:
CYEmptySet.gif
部分代码:

static char CYEmptySetCustomViewKey;

static char CYEmptySetContentViewKey;

static char CYEmptySetEmptyEnabledKey;

@interface UIScrollView ()

@property (nonatomic, weak) UIView *contentView;

@end

@implementation UIScrollView (CYEmptySet)

- (void)exchangeSeletor1:(SEL)selector1 selector2:(SEL)selector2 {
    Class class = [self class];
    Method originalMethod = class_getInstanceMethod(class, selector1);
    Method swizzledMethod = class_getInstanceMethod(class, selector2);
    
    // Change implementation
    BOOL success = class_addMethod(class, selector1, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
    if (success) {
        class_replaceMethod(class, selector2, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
    } else {
        method_exchangeImplementations(originalMethod, swizzledMethod);
    }
}

- (void)setContentView:(UIView *)contentView {
    objc_setAssociatedObject(self, &CYEmptySetContentViewKey, contentView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIView *)contentView {
    UIView *contentView = objc_getAssociatedObject(self, &CYEmptySetContentViewKey);
    if (!contentView) {
        contentView = [UIView new];
        contentView.backgroundColor = [UIColor clearColor];
        self.contentView = contentView;
    }
    contentView.translatesAutoresizingMaskIntoConstraints = false;
    return contentView;
}

- (void)cy_reloadData {
    [self cy_reloadData];
    
    if (!self.emptyEnabled) return;
    UIView *contentView = self.contentView;
    
    if ([self rowCount] == 0) {
        if (([self isKindOfClass:[UITableView class]] || [self isKindOfClass:[UICollectionView class]])
            && !contentView.superview
            && self.subviews.count > 1) {
            [self addSubview:contentView];
            [self insertSubview:contentView atIndex:0];
            if (self.customEmptyView) {
                self.customEmptyView.translatesAutoresizingMaskIntoConstraints = false;
                [contentView addSubview:self.customEmptyView];
            }
            [self addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView
                                                             attribute:NSLayoutAttributeWidth
                                                             relatedBy:NSLayoutRelationEqual
                                                                toItem:self
                                                             attribute:NSLayoutAttributeWidth
                                                            multiplier:1.0
                                                              constant:0.0]];
            
            [self addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView
                                                             attribute:NSLayoutAttributeHeight
                                                             relatedBy:NSLayoutRelationEqual
                                                                toItem:self
                                                             attribute:NSLayoutAttributeHeight
                                                            multiplier:1.0
                                                              constant:0.0]];
            
            [self addConstraintWithLayoutAttributes:@[@(NSLayoutAttributeLeft),
                                                      @(NSLayoutAttributeBottom),
                                                      @(NSLayoutAttributeRight)] view:self.contentView equalToSuperView:self];
            [self addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView
                                                             attribute:NSLayoutAttributeTop
                                                             relatedBy:NSLayoutRelationEqual
                                                                toItem:self
                                                             attribute:NSLayoutAttributeTop
                                                            multiplier:1.0 constant:-self.contentInset.top]];
            [self addConstraintWithLayoutAttributes:@[@(NSLayoutAttributeTop),
                                                      @(NSLayoutAttributeLeft),
                                                      @(NSLayoutAttributeBottom),
                                                      @(NSLayoutAttributeRight)] view:self.customEmptyView equalToSuperView:self.contentView];
            
            
            
        }
    } else {
        [contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        [contentView removeFromSuperview];
        self.contentView = nil;
    }
    
}

- (void)addConstraintWithLayoutAttributes:(NSArray *)attributes view:(UIView *)view equalToSuperView:(UIView *)superView {
    if (!superView || !view) return;
    for (NSNumber *attribute in attributes) {
        NSLayoutAttribute att = attribute.integerValue;
        NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:view
                                                                      attribute:att
                                                                      relatedBy:NSLayoutRelationEqual
                                                                         toItem:superView
                                                                      attribute:att
                                                                     multiplier:1.0
                                                                       constant:0.0];
        [superView addConstraint:constraint];
    }
}

- (NSInteger)rowCount {
    NSInteger rowCount = 0;
    if ([self isKindOfClass:[UITableView class]]) {
        UITableView *tableView = (UITableView *)self;
        for (NSInteger section = 0; section < tableView.numberOfSections; section++) {
            rowCount += [tableView numberOfRowsInSection:section];
        }
    } else if ([self isKindOfClass:[UICollectionView class]]) {
        UICollectionView *collectionView = (UICollectionView *)self;
        for (NSInteger section = 0; section < collectionView.numberOfSections; section++) {
            rowCount += [collectionView numberOfItemsInSection:section];
        }
    }
    return rowCount;
}

- (UIView *)customEmptyView {
    return objc_getAssociatedObject(self, &CYEmptySetCustomViewKey);
}

- (void)setCustomEmptyView:(UIView *)customEmptyView {
    objc_setAssociatedObject(self, &CYEmptySetCustomViewKey, customEmptyView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (BOOL)emptyEnabled {
    return [objc_getAssociatedObject(self, &CYEmptySetEmptyEnabledKey) boolValue];
}

- (void)setEmptyEnabled:(BOOL)emptyEnabled {
    if (emptyEnabled) {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            [self exchangeSeletor1:@selector(reloadData) selector2:@selector(cy_reloadData)];
        });
    }
    objc_setAssociatedObject(self, &CYEmptySetEmptyEnabledKey, @(emptyEnabled), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

Demo和代码下载地址:Demo和代码下载地址

相关文章

  • EmptySet:iOS列表占位处理

    描述: 列表没有数据、无网络状态下显示占位图处理 场景: 列表没有数据、无网络状态 原理: 捕捉UITableVi...

  • iOS Skeleton Screen加载占位图

    iOS Skeleton Screen加载占位图 iOS Skeleton Screen加载占位图

  • 字符串相关知识点

    字符串格式化所用占位符列表 转义字符 \ 运算 + :拼接 * :复制 字符串的处理 编码解码的问题

  • ios 使用本地通知

    IOS10系统 app没有出现在系统设置-通知列表处理

  • iOS占位图、空白页面

    iOS占位图、空白页面 Github直达 iOS占位图、空白页面 适用于view、tableView、collec...

  • IOS开合列表

    一款ios开合列表 本开合列表依据数据处理实现的开合,所以代码量极少,页面处理简洁,轻松实现列表开合 项目地址:h...

  • 小程序 列表与条件渲染并用问题记录

    这样的需求想必所有应用都有: 进入界面后,当列表无数据,显示暂无数据占位图,有数据则隐藏占位图显示列表数据。 遇到...

  • 开发资料

    开源ReactNative项目列表 学习资料-书籍 iOS文章列表 iOS开源列表 Awesome iOS A c...

  • 随记

    MySQL与 PostgreSQL 预处理(Prepared) 匿名占位符 MySQL: 使用 ? 作为匿名占位符...

  • 修改UITextField的placeholder 字体颜色大小

    UITextField 是 iOS 开发中常用的控件,它有一个placeholder属性,也就是占位文字,默认占位...

网友评论

    本文标题:EmptySet:iOS列表占位处理

    本文链接:https://www.haomeiwen.com/subject/dbcdzttx.html