上海快风信息科技有限公司iOS Developer - RuntimeiOS Developer - UITableView

简单小技巧给UITableView添加没数据时的提示

2016-09-12  本文已影响993人  青春微凉来时路

欢迎加入 iOS开发QQ群:151133690

按照惯例还是先来两张效果图吧.

设置加载数据中... 获取数据失败,重置数据

使用很简单,只需在需要设置的地方赋值就行

self.tableView.placeholder = @"请检查网络连接";

使用方法

新建一个扩展,然后把.h和.m拷贝进去就行了,
当然也可以根据需要自己修改想要的格式,
这里只是文字的显示,提供一个思路,
如果想要显示图片,请自行修改.

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 -- 重新设置数据
-(void)reloadPlaceoholderLabel
{
    [self.placeholderLabel removeFromSuperview];
    
    //计算行数
    NSInteger rows = 0;
    for (int i = 0; i <  self.numberOfSections; i ++) {
        
        rows += [self numberOfRowsInSection:i];
    }
    
    //如果没有数据 或者字符串为空 就不显示
    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;
    }
    
    //显示提示内容的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
如果需要显示图片 获其他内容,可以在.m文件中 自己修改.

-(void)reloadPlaceoholderLabel 方法重置数据 
每次reload delete 或insert之后都会调用
上一篇下一篇

猜你喜欢

热点阅读