【iOS/runtime/002】Swizzling 方法交换

Swizzling 方法交换

TableView 默认空页面制作

// UITableView+ex.h
#import <UIKit/UIKit.h>
#import <objc/runtime.h>

@interface UITableView (ex)

@property(strong, nonatomic)UIView *lgValue;

@end
// UITableView+ex.m
#import "UITableView+ex.h"

#define LGDefaultView "LGDefaultView"

@implementation UITableView (ex)
+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Method originMethod = class_getInstanceMethod(self, @selector(reloadData));
        Method currentMethod = class_getInstanceMethod(self, @selector(lg_reloadData));
        method_exchangeImplementations(originMethod, currentMethod);
    });
}

- (void)lg_reloadData {
    // 调用系统的 reloadData
    [self lg_reloadData];
}

- (void)fullDefaultView {
    id<UITableViewDataSource> dataSource = self.dataSource;
    NSInteger section = [dataSource respondsToSelector:@selector(numberOfSectionsInTableView:)] ? [dataSource numberOfSectionsInTableView:self] : 1;
    NSInteger rows = 0;
    for (NSInteger i = 0; i < section; i++) {
        rows += [dataSource tableView:self numberOfRowsInSection:i];
    }

    if (!rows) {
        self.lgValue = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 55, 55)];
        self.lgValue.backgroundColor = [UIColor redColor];
        [self addSubview:self.lgValue];
    } else {
        [self.lgValue removeFromSuperview];
    }
}

#pragma mark -- GETTE AND SETTER
- (void)setLgValue:(UIView *)lgValue {
    objc_setAssociatedObject(self, LGDefaultView, lgValue, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIView *)getLgValue {
    return objc_getAssociatedObject(self, LGDefaultView);
}

@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值