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

  • 相关阅读:
    如何添加“写字板”打开方式
    UML类图聚集与组合的区别
    系统调用跟驱动程序中相应函数的参数对应关系
    PHP 判断数据类型
    PHP连接MySQL数据库的三种方式(mysql、mysqli、pdo)
    java 中的内部类总结
    cross-env使用笔记
    MySQL——约束(constraint)详解
    MySQL数据库--外键约束及外键使用
    Java中Lambda表达式的使用
  • 原文地址:https://www.cnblogs.com/happyEveryData/p/5546757.html
Copyright © 2011-2022 走看看