zoukankan      html  css  js  c++  java
  • Custom-->TableView_Swizzle

    #import "UITableView+Swizzle.h"

    #import <UIKit/UIKit.h>

    #import <objc/objc.h>

    #import <objc/runtime.h>

    @implementation UITableView (Swizzle)

    实现:一次性设置tableView的位置

    + (void)load

    {

        static dispatch_once_t onceToken;

        dispatch_once(&onceToken, ^{

            [self swizzleInstanceMethod:@selector(initWithFrame:style:) withNewMethod:@selector(yq_initWithFrame:style:)];// 在这里替换

        });

    }

    - (instancetype)yq_initWithFrame:(CGRect)frame style:(UITableViewStyle)style

    {

        UITableView *tableView = [self yq_initWithFrame:frame style:style];

        [tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];

        return tableView;

    }

    /**

     *  替换实例方法(对象方法)

     *

     *  @param originalSel 原方法SEL

     *  @param newSel      新方法SEL

     */

    + (BOOL)swizzleInstanceMethod:(SEL)originalSel withNewMethod:(SEL)newSel

    {

        Method originalMethod = class_getInstanceMethod(self, originalSel);

        Method newMethod = class_getInstanceMethod(self, newSel);

        if (!originalMethod || !newMethod) return NO;

        class_addMethod(self, originalSel,  class_getMethodImplementation(self, originalSel), method_getTypeEncoding(originalMethod));

        class_addMethod(self, newSel, class_getMethodImplementation(self, newSel),  method_getTypeEncoding(newMethod));

        method_exchangeImplementations(class_getInstanceMethod(self, originalSel), class_getInstanceMethod(self, newSel));

        return YES;

    }

    TableViewCell_Swizzle 

    #import "UITableViewCell+Swizzle.h"

    @implementation UITableViewCell (Swizzle)

    + (void)load

    {

        static dispatch_once_t onceToken;

        dispatch_once(&onceToken, ^{

            [self swizzleClassMethod:@selector(initWithStyle:reuseIdentifier:) withNewMethod:@selector(yq_initWithStyle:reuseIdentifier:)];

        });

    }

    - (instancetype)yq_initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

    {

        UITableViewCell *cell = [self yq_initWithStyle:style reuseIdentifier:reuseIdentifier];

        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

        return cell;

    }

    @end

  • 相关阅读:
    C# 中类重写 ToString 方法
    虚方法(virtual)和抽象方法(abstract)的区别
    C#的扩展方法学习
    C# .NET 和.NET Framework区别
    C#值类型和引用类型
    抽象和接口的区别和使用
    什么是委托?
    什么是继承?
    设计模式:单一职责原则
    Java 13,最新最全新特性解读
  • 原文地址:https://www.cnblogs.com/happyEveryData/p/5546757.html
Copyright © 2011-2022 走看看