Coder Social home page Coder Social logo

jxcellseparator's Introduction

JXCellSeparator

嗯。。。封装了一下cell的系统分割线调用方法,简单易用。

JXCellSeparator

  • 使用示例
#import "UITableViewCell+JXSeparator.h"

@interface ViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"JXSeparator";
    self.tableView.frame = self.view.bounds;
    [self.view addSubview:self.tableView];
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 30;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([UITableViewCell class])];
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        // 配置各种风格下的间距
        cell.jx_separatorCenterMargin = 15;
        cell.jx_separatorLeftMargin = 50;
        cell.jx_separatorRightMargin = 30;
    }
    
    NSString *styleString = nil;
    if (indexPath.row % 5 == 0) {
        cell.jx_separatorStyle = JXSeparatorStyleCenter;
        styleString = @"Center";
    }
    else if (indexPath.row % 5 == 1) {
        cell.jx_separatorStyle = JXSeparatorStyleFull;
        styleString = @"Full";
    }
    else if (indexPath.row % 5 == 2) {
        cell.jx_separatorStyle = JXSeparatorStyleLeftMargin;
        styleString = @"LeftMargin";
    }
    else if (indexPath.row % 5 == 3) {
        cell.jx_separatorStyle = JXSeparatorStyleRightMargin;
        styleString = @"RightMargin";
    }
    else {
        cell.jx_separatorStyle = JXSeparatorStyleNone;
        styleString = @"None";
    }
    cell.textLabel.text = [NSString stringWithFormat:@"%@ Row:%@", styleString, @(indexPath.row + 1)];
    return cell;
}

@end
  • UITableViewCell+JXSeparator.h
typedef NS_ENUM(NSUInteger, JXSeparatorStyle) {
    JXSeparatorStyleNone = 0,   //
    JXSeparatorStyleFull,       // 满行
    JXSeparatorStyleCenter,     // 两边留出间距
    JXSeparatorStyleLeftMargin, // 左边留出间距
    JXSeparatorStyleRightMargin,// 右边留出间距
};

@interface UITableViewCell (JXSeparator)

/// 分割线风格
@property (nonatomic, assign) JXSeparatorStyle jx_separatorStyle;

/// 两边等间距
@property (nonatomic, assign) CGFloat jx_separatorCenterMargin;

/// 左边间距
@property (nonatomic, assign) CGFloat jx_separatorLeftMargin;

/// 右边间距
@property (nonatomic, assign) CGFloat jx_separatorRightMargin;

@end
  • UITableViewCell+JXSeparator.m
#import <objc/runtime.h>

static const char kJXSeparatorMarginKey = '\0';
static const char kJXSeparatorLeftMarginKey = '\0';
static const char kJXSeparatorRightMarginKey = '\0';
static const char kJXSeparatorStyleKey = '\0';

@implementation UITableViewCell (JXSeparator)

#pragma 左间距
- (CGFloat)jx_separatorLeftMargin {
    NSNumber *leftMargin = objc_getAssociatedObject(self, &kJXSeparatorLeftMarginKey);
    return leftMargin ? [leftMargin floatValue] : 0;
}

- (void)setJx_separatorLeftMargin:(CGFloat)jx_separatorLeftMargin {
    objc_setAssociatedObject(self, &kJXSeparatorLeftMarginKey, @(jx_separatorLeftMargin), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

#pragma 右间距
- (CGFloat)jx_separatorRightMargin {
    NSNumber *rightMargin = objc_getAssociatedObject(self, &kJXSeparatorRightMarginKey);
    return rightMargin ? [rightMargin floatValue] : 0;
}

- (void)setJx_separatorRightMargin:(CGFloat)jx_separatorRightMargin {
    objc_setAssociatedObject(self, &kJXSeparatorRightMarginKey, @(jx_separatorRightMargin), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

#pragma 两边间距
- (CGFloat)jx_separatorCenterMargin {
    NSNumber *margin = objc_getAssociatedObject(self, &kJXSeparatorMarginKey);
    return margin ? [margin floatValue] : 0;
}

- (void)setJx_separatorCenterMargin:(CGFloat)jx_separatorCenterMargin {
    objc_setAssociatedObject(self, &kJXSeparatorMarginKey, @(jx_separatorCenterMargin), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

#pragma mark - 风格
- (JXSeparatorStyle)jx_separatorStyle {
    NSNumber *style = objc_getAssociatedObject(self, &kJXSeparatorStyleKey);
    return style ? [style integerValue] : 0;
}

- (void)setJx_separatorStyle:(JXSeparatorStyle)jx_separatorStyle {
    objc_setAssociatedObject(self, &kJXSeparatorStyleKey, @(jx_separatorStyle), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
    if ([[UIDevice currentDevice].systemVersion compare:@"8.0"] != NSOrderedAscending) {
        self.layoutMargins = UIEdgeInsetsZero;
        self.preservesSuperviewLayoutMargins = NO;
    }
    
    switch (jx_separatorStyle) {
        case JXSeparatorStyleNone:
            self.separatorInset = UIEdgeInsetsMake(0, 0, 0, self.bounds.size.width);
            break;
        case JXSeparatorStyleFull:
            self.separatorInset = UIEdgeInsetsZero;
            break;
        case JXSeparatorStyleLeftMargin:
            self.separatorInset = UIEdgeInsetsMake(0, self.jx_separatorLeftMargin, 0, 0);
            break;
        case JXSeparatorStyleRightMargin:
            self.separatorInset = UIEdgeInsetsMake(0, 0, 0, self.jx_separatorRightMargin);
            break;
        case JXSeparatorStyleCenter:
            self.separatorInset = UIEdgeInsetsMake(0, self.jx_separatorCenterMargin, 0, self.jx_separatorCenterMargin);
            break;
        default:
            break;
    }
}

@end

jxcellseparator's People

Contributors

jiongxing avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.