簡單小技巧給UITableView添加沒數(shù)據(jù)時的提示

歡迎加入 iOS開發(fā)QQ群:151133690

按照慣例還是先來兩張效果圖吧.

設置加載數(shù)據(jù)中...
獲取數(shù)據(jù)失敗,重置數(shù)據(jù)

使用很簡單,只需在需要設置的地方賦值就行

self.tableView.placeholder = @"請檢查網(wǎng)絡連接";

使用方法

新建一個擴展,然后把.h和.m拷貝進去就行了,
當然也可以根據(jù)需要自己修改想要的格式,
這里只是文字的顯示,提供一個思路,
如果想要顯示圖片,請自行修改.

UITableView+Empty.h文件

//
//  UITableView+Empty.h
//  Up
//
//  Created by it3部01 on 16/9/12.
//  Copyright ? 2016年 benben. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface UITableView (Empty)

@property (nonatomic,copy) NSString *placeholder; /**< 提示字符串 */

@end

UITableView+Empty.m文件

//
//  UITableView+Empty.m
//  Up
//
//  Created by it3部01 on 16/9/12.
//  Copyright ? 2016年 benben. All rights reserved.
//

#import "UITableView+Empty.h"
#import <objc/runtime.h>

@interface UITableView ()

@property (nonatomic,strong) UILabel *placeholderLabel; /**< 占位文字label */

@end

@implementation UITableView (Empty)

+(void)load
{
    //交換reloadData方法
    method_exchangeImplementations(class_getInstanceMethod(self, @selector(reloadData)), class_getInstanceMethod(self, @selector(lx_reloadData)));
    
    //交換insertSections方法
    method_exchangeImplementations(class_getInstanceMethod(self,@selector(insertSections:withRowAnimation:)),class_getInstanceMethod(self,@selector(lx_insertSections:withRowAnimation:)));
    
    //交換insertRowsAtIndexPaths方法
    method_exchangeImplementations(class_getInstanceMethod(self, @selector(insertRowsAtIndexPaths:withRowAnimation:)),  class_getInstanceMethod(self, @selector(lx_insertRowsAtIndexPaths:withRowAnimation:)));
    
    //交換deleteSections方法
    method_exchangeImplementations(class_getInstanceMethod(self,@selector(deleteSections:withRowAnimation:)),class_getInstanceMethod(self,@selector(lx_deleteSections:withRowAnimation:)));
    
    //交換deleteRowsAtIndexPaths方法
    method_exchangeImplementations(class_getInstanceMethod(self,@selector(deleteRowsAtIndexPaths:withRowAnimation:)),class_getInstanceMethod(self,@selector(lx_deleteRowsAtIndexPaths:withRowAnimation:)));
}

//reloadData
-(void)lx_reloadData
{
    [self lx_reloadData];
    
    [self reloadPlaceoholderLabel];
}

//insert
-(void)lx_insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation
{
    [self lx_insertSections:sections withRowAnimation:animation];
    [self reloadPlaceoholderLabel];
}

-(void)lx_insertRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
{
    [self lx_insertRowsAtIndexPaths:indexPaths withRowAnimation:animation];
    [self reloadPlaceoholderLabel];
}

//delete
-(void)lx_deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation
{
    [self lx_deleteSections:sections withRowAnimation:animation];
    [self reloadPlaceoholderLabel];
}

-(void)lx_deleteRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
{
    [self lx_deleteRowsAtIndexPaths:indexPaths withRowAnimation:animation];
    [self reloadPlaceoholderLabel];
}
                                   
#pragma mark -- 重新設置數(shù)據(jù)
-(void)reloadPlaceoholderLabel
{
    [self.placeholderLabel removeFromSuperview];
    
    //計算行數(shù)
    NSInteger rows = 0;
    for (int i = 0; i <  self.numberOfSections; i ++) {
        
        rows += [self numberOfRowsInSection:i];
    }
    
    //如果沒有數(shù)據(jù) 或者字符串為空 就不顯示
    if (rows > 0 || !self.placeholder) {
        
        return;
    }
    
    //是否有偏移
    CGFloat height = self.contentInset.top;
    
    //判斷是否有頭
    if (self.tableHeaderView) {
        height += self.tableHeaderView.height;
    }
    
    if (height < self.height/2.0-100) {
        height = self.height/2.0-100;
    }else {
        height += 30;
    }
    
    //顯示提示內(nèi)容的label
    self.placeholderLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, height, self.width-40, 60)];
    self.placeholderLabel.numberOfLines = 3;
    self.placeholderLabel.text = self.placeholder;
    self.placeholderLabel.font = [UIFont boldSystemFontOfSize:20.0];
    self.placeholderLabel.textColor = [UIColor lightGrayColor];
    self.placeholderLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.placeholderLabel];
}

#pragma mark -- getter & setter
-(NSString *)placeholder
{
    return objc_getAssociatedObject(self, @selector(placeholder));
}

-(void)setPlaceholder:(NSString *)placeholder
{
    objc_setAssociatedObject(self, @selector(placeholder), placeholder, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
    [self reloadPlaceoholderLabel];
}

-(UILabel *)placeholderLabel
{
    return objc_getAssociatedObject(self, @selector(placeholderLabel));
}

-(void)setPlaceholderLabel:(UILabel *)placeholderLabel
{
    objc_setAssociatedObject(self, @selector(placeholderLabel), placeholderLabel, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end

說明

因為我這邊只顯示文字提示 所以就建了一個placeholderLabel
如果需要顯示圖片 獲其他內(nèi)容,可以在.m文件中 自己修改.

-(void)reloadPlaceoholderLabel 方法重置數(shù)據(jù) 
每次reload delete 或insert之后都會調(diào)用
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

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

  • 發(fā)現(xiàn) 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 15,028評論 4 61
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,725評論 25 709
  • 我們長大了 便不會再讓自己輕易的受傷 我們丟掉了好多東西 純真,脆弱,倔強或者清高 有的找到了自己的小天空 日復一...
    慕言澈閱讀 279評論 1 1
  • 平安校園: 你好! 寶寶好害怕,好傷心……曾多么天真地認為校園很安全,沒有壞人,然而,當丟了手機之后,...
    小丫嘛小藝閱讀 446評論 0 1
  • 夢想 有夢想 人要有夢想
    麗麗我我閱讀 234評論 0 0

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