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

 

posted on 2016-05-31 17:46  Evan*少  阅读(156)  评论(0编辑  收藏  举报

导航