歡迎加入 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)用