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

  • 相关阅读:
    Java实现 计蒜客 拯救行动
    Java实现 计蒜客 拯救行动
    Java实现 LeetCode 174 地下城游戏
    Java实现 LeetCode 174 地下城游戏
    Java实现 LeetCode 174 地下城游戏
    Java实现 LeetCode 173 二叉搜索树迭代器
    Java实现 LeetCode 173 二叉搜索树迭代器
    Visual Studio的SDK配置
    怎样使用CMenu类
    mfc menu用法一
  • 原文地址:https://www.cnblogs.com/happyEveryData/p/5546757.html
Copyright © 2011-2022 走看看