iOS 修改UITableView separator

separator style

UITableView 中的 separator 有三种类型:


typedef NS_ENUM(NSInteger, UITableViewCellSeparatorStyle) {

UITableViewCellSeparatorStyleNone, // 没有分割线

UITableViewCellSeparatorStyleSingleLine, // 单线,默认

UITableViewCellSeparatorStyleSingleLineEtched  // 内嵌线,只有在 UITableView 为 group 类型时才起作用

}

通过修改 UITableView 的 separatorStyle 属性修改 separator 的类型,通过修改 separatorColor 属性修改 separator 的颜色,通过修改 separatorEffect( iOS 8.0 之后) 属性修改 separator 的显示特效。


separator inset

修改 separator 到边缘距离的时候,只需要修改 UITableView 的 separatorInset 属性就可以了,例如,当使用 xib 或 storyBoard 定制 cell 的时候,修改 tableview 的 Separator Inset 为 Custom,然后修改左右边距。但是最近突然发现这样完全不起作用了,即使手动修改 separatorInset 为 UIEdgeInsetsZero 也不起作用,具体什么原因没有细究,然后通过万能的百度,找到了解决方法:

方法一:


- (void)viewDidLoad {

[super viewDidLoad];

...

...

...

#pragma mark - a 调整view边距

// 1.调整(iOS7以上)表格分隔线边距

if ([self.MyTableView respondsToSelector:@selector(setSeparatorInset:)]) {

self.MyTableView.separatorInset = UIEdgeInsetsZero;

}

// 2.调整(iOS8以上)view边距(或者在cell中设置preservesSuperviewLayoutMargins,二者等效)

if ([self.MyTableView respondsToSelector:@selector(setLayoutMargins:)]) {

self.MyTableView.layoutMargins = UIEdgeInsetsZero;

}

}

#pragma mark - b 调整view边距

//然后在willDisplayCell方法中加入如下代码:

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

#pragma mark - b

if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {

[cell setLayoutMargins:UIEdgeInsetsZero];

}

}

方法二:


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

...

...

...

#pragma mark - a 调整view边距

//1.调整(iOS8以上)tableView边距(与上面第2步等效,二选一即可)

if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {

cell.preservesSuperviewLayoutMargins = NO;

}

//2.调整(iOS8以上)view边距

if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {

[cell setLayoutMargins:UIEdgeInsetsZero];

}

return cell;

}

#pragma mark - b 调整view边距

//然后在willDisplayCell方法中加入如下代码:

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

#pragma mark - b

if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {

[cell setSeparatorInset:UIEdgeInsetsZero];

}

}

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 版权声明:未经本人允许,禁止转载. 1. TableView初始化 1.UITableView有两种风格:UITa...
    萧雪痕阅读 3,027评论 2赞 10
  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 9,429评论 3赞 38
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,768评论 30赞 472
  • { 24、Sqlite数据库 1、存储大数据量,增删改查,常见管理系统:Oracle、MSSQLServer、DB...
    CYC666阅读 1,114评论 0赞 1
  • 一图抵千言,说的就是有时候一张图的表达结果,强过你说千百句话。这一点都不意外,我们天生就长着擅长视觉的大脑,形状的...
    过客阅读 451评论 0赞 0

友情链接更多精彩内容