iOS 一行代码实现tableView占位图

@interface UITableView (PlaceholderView)

@property(nonatomic,strong)UIView *emptyView;

@end
#import "UITableView+PlaceholderView.h"
#import <objc/runtime.h>

@implementation UITableView (PlaceholderView)

#pragma mark ----------------------- emptyView set 方法 ------------------------
-(void)setEmptyView:(UIView *)emptyView
{
    objc_setAssociatedObject(self, @selector(emptyView), emptyView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
    if (objc_getAssociatedObject(self, @selector(emptyView)))
    {
        //防止手动调用load出现多次调用的情况:
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            
            NSLog(@"刷新啊啊");
            [UITableView swizzleOriginMethod:@selector(reloadData)  Method:@selector(newReloadData)];
        });
    }
}

#pragma mark ----------------------- 自定义刷新方法 ------------------------
-(void)newReloadData
{
    [self checkIsEmpty];
    [self newReloadData];
}

#pragma mark ----------------------- emptyView 懒加载 ------------------------
-(UIView *)emptyView
{
    return objc_getAssociatedObject(self, @selector(emptyView));
}

#pragma mark ----------------------- 替换系统刷新 ------------------------
+ (void)swizzleOriginMethod:(SEL)oriSel Method:(SEL)newSel
{
    Method oldMethod = class_getInstanceMethod(self, oriSel);//旧方法
    Method newMethod = class_getInstanceMethod(self, newSel);//新方法
    BOOL methodIsAdd = class_addMethod(self, oriSel, method_getImplementation(newMethod), method_getTypeEncoding(newMethod));
    if (methodIsAdd)
    {
        class_replaceMethod(self, newSel, method_getImplementation(oldMethod), method_getTypeEncoding(oldMethod));
    }
    else
    {
        method_exchangeImplementations(oldMethod, newMethod);
    }
}

#pragma mark ----------------------- 新刷新 ------------------------
- (void)checkIsEmpty
{
    BOOL isEmpty = YES;
    
    if ([self.dataSource respondsToSelector:@selector(numberOfSectionsInTableView:)])
    {
        NSInteger sections = [self.dataSource numberOfSectionsInTableView:self];
        if (sections > 0)
        {
            isEmpty = NO;
        }
    }
    else
    {
        NSInteger rows = [self.dataSource tableView:self numberOfRowsInSection:0];
        if (rows > 0)
        {
            isEmpty = NO;
        }
    }
    if (isEmpty)
    {
        [self.emptyView removeFromSuperview];
        [self addSubview:self.emptyView];
    }
    else
    {
        [self.emptyView removeFromSuperview];
    }
}

@end
// 调用
#import "UITableView+PlaceholderView.h"

@interface ViewController ()
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    UIView *placeholderView = [[UIView alloc]initWithFrame:self.view.bounds];
    placeholderView.backgroundColor = [UIColor redColor];

    [self.view addSubview:self.tableView];
    
    
    self.tableView.emptyView = placeholderView;
    
}

-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.bounds];
        _tableView.backgroundColor = [UIColor blueColor];
    }
    return _tableView;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值