EmptySet:iOS列表占位處理

描述:

列表沒有數(shù)據(jù)、無網(wǎng)絡(luò)狀態(tài)下顯示占位圖處理

場(chǎng)景:

列表沒有數(shù)據(jù)、無網(wǎng)絡(luò)狀態(tài)

原理:

捕捉UITableView/UICollectionView的reloadData事件,判斷列表有無數(shù)據(jù),處理展示占位圖邏輯

介紹:
  1. EmpySet使用分類UIScrollView+CYEmptySet實(shí)現(xiàn)
  2. 對(duì)無任何侵入性,不依賴于任何三方庫
使用:
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和代碼下載地址
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,022評(píng)論 4 61
  • 人屬于群居性動(dòng)物,所以個(gè)人在沒有決策,或者即使擁有有個(gè)人的想法,但只要有人開了頭,我們就會(huì)很快放棄自己的想法,從了...
    順鍋閱讀 240評(píng)論 2 1
  • 我從不歌頌?zāi)切槟腥艘赖墓媚镂屹澝滥欠N知道愛情離去后擦干臉上的淚轉(zhuǎn)身就走的女孩又冷又酷又美像西部片里的牛仔崩...
    如煙魚閱讀 474評(píng)論 1 3
  • “無法說出口的愛情?!? 《戴上手套擦淚》像是一群人的編年史,瑞典版分別叫做愛,病和死,中文版改得溫和了許多...
    南蟬閱讀 1,380評(píng)論 0 0

友情鏈接更多精彩內(nèi)容