EmptySet:iOS列表占位處理

描述:

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

場景:

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

原理:

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

介紹:
  1. EmpySet使用分類UIScrollView+CYEmptySet實現(xiàn)
  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和代碼下載地址
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末秕噪,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子厚宰,更是在濱河造成了極大的恐慌腌巾,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,820評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件固阁,死亡現(xiàn)場離奇詭異壤躲,居然都是意外死亡,警方通過查閱死者的電腦和手機备燃,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,648評論 3 399
  • 文/潘曉璐 我一進(jìn)店門碉克,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人并齐,你說我怎么就攤上這事漏麦。” “怎么了况褪?”我有些...
    開封第一講書人閱讀 168,324評論 0 360
  • 文/不壞的土叔 我叫張陵撕贞,是天一觀的道長。 經(jīng)常有香客問我测垛,道長捏膨,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,714評論 1 297
  • 正文 為了忘掉前任食侮,我火速辦了婚禮号涯,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘锯七。我一直安慰自己链快,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 68,724評論 6 397
  • 文/花漫 我一把揭開白布眉尸。 她就那樣靜靜地躺著域蜗,像睡著了一般。 火紅的嫁衣襯著肌膚如雪噪猾。 梳的紋絲不亂的頭發(fā)上霉祸,一...
    開封第一講書人閱讀 52,328評論 1 310
  • 那天,我揣著相機與錄音袱蜡,去河邊找鬼丝蹭。 笑死,一個胖子當(dāng)著我的面吹牛戒劫,可吹牛的內(nèi)容都是我干的半夷。 我是一名探鬼主播,決...
    沈念sama閱讀 40,897評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼迅细,長吁一口氣:“原來是場噩夢啊……” “哼巫橄!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起茵典,我...
    開封第一講書人閱讀 39,804評論 0 276
  • 序言:老撾萬榮一對情侶失蹤湘换,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后统阿,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體彩倚,經(jīng)...
    沈念sama閱讀 46,345評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,431評論 3 340
  • 正文 我和宋清朗相戀三年扶平,在試婚紗的時候發(fā)現(xiàn)自己被綠了帆离。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,561評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡结澄,死狀恐怖哥谷,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情麻献,我是刑警寧澤们妥,帶...
    沈念sama閱讀 36,238評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站勉吻,受9級特大地震影響监婶,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜齿桃,卻給世界環(huán)境...
    茶點故事閱讀 41,928評論 3 334
  • 文/蒙蒙 一惑惶、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧源譬,春花似錦集惋、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,417評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至养渴,卻和暖如春雷绢,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背理卑。 一陣腳步聲響...
    開封第一講書人閱讀 33,528評論 1 272
  • 我被黑心中介騙來泰國打工翘紊, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人藐唠。 一個月前我還...
    沈念sama閱讀 48,983評論 3 376
  • 正文 我出身青樓帆疟,卻偏偏與公主長得像鹉究,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子踪宠,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,573評論 2 359

推薦閱讀更多精彩內(nèi)容

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