【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+e
·
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
更多推荐
已为社区贡献1条内容
所有评论(0)