TableViewCell分割线的解决方案

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

JXCellSeparator
JXCellSeparator
  • 使用示例
//
//  ViewController.m
//  JXCellSeparator
//
//  Created by JiongXing on 16/8/26.
//  Copyright © 2016年 JiongXing. All rights reserved.
//

#import "ViewController.h"
#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
//
//  UITableViewCell+JXSeparator.h
//  JXCellSeparator
//
//  Created by JiongXing on 16/8/26.
//  Copyright © 2016年 JiongXing. All rights reserved.
//

#import <UIKit/UIKit.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
//
//  UITableViewCell+JXSeparator.m
//  JXCellSeparator
//
//  Created by JiongXing on 16/8/26.
//  Copyright © 2016年 JiongXing. All rights reserved.
//

#import "UITableViewCell+JXSeparator.h"
#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

参考资料:由UITableView分割线说开去

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,305评论 4 61
  • 或许太疲惫了,伴随大雨回到住处!简单洗漱,躺在床上,不知不觉就进入梦乡! 不知这是哪里的僻静之所,不知哪里来的世外...
    安静的松鼠阅读 145评论 0 0
  • 这个姑娘 一直被别人觉得是一个很直接的人,很多话她都敢直接说出口。 小时候有这样一件事情。大约是五年级或者六年级,...
    小肉丸子阅读 323评论 4 3
  • 我比平时的周末都要早起,我有好多事情没有干,比如,给集团公司提交数据,其实我是不愿意在休息的时候干工作的事,但又不...
    静妥妥阅读 207评论 0 0
  • 今天我去辅导班,美术老师教我们画稻草人,先画一顶帽子,然后再换一张半圆形的脸,然后在画上衣服,在画胳膊,...
    丁子涵阅读 225评论 0 0