使用runtime 自动补齐UITableViewCell分割线

首先创建一个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会先走分类方法哦

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值