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/c++ 传统数组的缺点
    在Eclipse中搭建C/C++环境
    web服务编码设置
    锋利的jQuery学习总结
    SQL调优常用方法
    C#(KeyChar和KeyCord值,KeyDown/KeyPress事件区别)
    winform 屏蔽 空格键
    $.ajax与$.post、$.get的一点区别
    jquery $.ajax $.get $.post的区别?
    $(function() {}),即$(document).ready(function(),什么时候执行?以此为准
  • 原文地址:https://www.cnblogs.com/happyEveryData/p/5546757.html
Copyright © 2011-2022 走看看