UITableView 隐藏最后一条分割线

UITableView 隐藏最后一条分割线-->footerView高度0.1)

ESSeparatorInset分类一行代码搞定

UIView

UIViewController+ESSeparatorInset.h:


// The MIT License (MIT)
//
// Copyright (c) 2015 EnjoySR ( https://github.com/EnjoySR/ESSeparatorInset )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

//
//  UIViewController+ESSeparatorInset.h
//  TableViewSeparatorInset
//
//  Created by  on 15/7/18.
//  Copyright (c) 2015年 EnjoySR. All rights reserved.
//

#import 

@interface UIViewController (ESSeparatorInset)

- (void)setSeparatorInsetZeroWithTableView:(UITableView *)tableView;

- (void)setSeparatorInsetWithTableView:(UITableView *)tableView inset:(UIEdgeInsets)inset;

/*
 If you want to implement 'tableView:willDisplayCell:forRowAtIndexPath:' for 
 tableview delegate,you should use this method.
 */
- (void)es_tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;

@end

UIViewController+ESSeparatorInset.m:

//
//  UIViewController+ESSeparatorInset.m
//  TableViewSeparatorInset
//
//  Created by 尹桥印 on 15/7/18.
//  Copyright (c) 2015年 EnjoySR. All rights reserved.
//

#import "UIViewController+ESSeparatorInset.h"
#import 

static NSString *ES_INSETS_ASS_KEY = @"ES_INSETS_ASS_KEY";

@interface UIViewController()

@property (nonatomic, assign) UIEdgeInsets inset;

@end

@implementation UIViewController (ESSeparatorInset)

+ (void)load
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class class = [self class];
        
        SEL originalSelector = @selector(tableView:willDisplayCell:forRowAtIndexPath:);
        SEL swizzledSelector = @selector(es_tableView:willDisplayCell:forRowAtIndexPath:);
        
        Method originalMethod = class_getInstanceMethod(class, originalSelector);
        Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
        
        BOOL success = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
        if (success) {
            class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
        } else {
            method_exchangeImplementations(originalMethod, swizzledMethod);
        }
    });
}

- (void)es_tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([self respondsToSelector:@selector(es_tableView:willDisplayCell:forRowAtIndexPath:)]) {
        [self es_tableView:tableView willDisplayCell:cell forRowAtIndexPath:indexPath];
    }
    [self setSeparatorInsetWithTarget:cell insets:self.inset];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
}

- (void)setSeparatorInsetZeroWithTableView:(UITableView *)tableView{
    [self setSeparatorInsetWithTableView:tableView inset:UIEdgeInsetsZero];
}

- (void)setSeparatorInsetWithTableView:(UITableView *)tableView inset:(UIEdgeInsets)inset{
    self.inset = inset;
    [self setSeparatorInsetWithTarget:tableView insets:inset];
}

- (void)setSeparatorInsetWithTarget:(id)target insets:(UIEdgeInsets)insets{
    if ([target respondsToSelector:@selector(setSeparatorInset:)]) {
        [target setSeparatorInset:insets];
    }
    if ([target respondsToSelector:@selector(setLayoutMargins:)]) {
        [target setLayoutMargins:insets];
    }
}

- (void)setInset:(UIEdgeInsets)insets{
    NSValue *value = [NSValue valueWithUIEdgeInsets:insets];
    objc_setAssociatedObject(self, &ES_INSETS_ASS_KEY, value, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIEdgeInsets)inset{
    NSValue *value = objc_getAssociatedObject(self, &ES_INSETS_ASS_KEY);
    return [value UIEdgeInsetsValue];
}

@end



你可能感兴趣的:(UITableView 隐藏最后一条分割线)