首先创建一个UITableViewCell的分类并导入#import <objc/runtime.h>
二话不说,上来就贴代码(实质上是利用自定义方法替换系统原生方法:方法替换)
#import "UITableViewCell+TabCellCategory.h"
#import <objc/runtime.h>
@implementation UITableViewCell (TabCellCategory)
+ (void)load{
[super load];
//u一次创建,一劳永逸
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
SEL originalSelector = @selector(layoutSubviews);//原始方法
SEL customerSelector = @selector(xhz_layoutSubviews);//自定义方法
//获取系统需要替换方法
Method originalMethod = class_getInstanceMethod([self class], originalSelector);
//创建替换方法
Method customerMethod = class_getInstanceMethod([self class], customerSelector);
//判断是否替换成功
BOOL didAddMethod = class_addMethod([self class], originalSelector, method_getImplementation(customerMethod), method_getTypeEncoding(customerMethod));
if (didAddMethod) {
class_replaceMethod([self class], customerSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
}else{
method_exchangeImplementations(originalMethod, customerMethod);
}
});
}
- (void)xhz_layoutSubviews{
//这里一定要调一次哦
[self xhz_layoutSubviews];
//补全分割线
self.separatorInset = UIEdgeInsetsZero;
//这个是设置cell的选中方式,您也可改变cell的其他属性
self.selectionStyle = UITableViewCellSelectionStyleNone;
}
@end
用法:不需要调用,因为调用tableViewCell会先走分类方法哦