一、问题描述

UITableView分割线要显示到最左端

查看UITableView的属性,发现设置separatorInset的值可以自定义分割线的位置。

@property (nonatomic) UIEdgeInsets separatorInset NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR; // allows customization of the frame of cell separators

打印separatorInset,其默认值{0, 15, 0, 0},上、左、下、右距离原位置分别为0、15、0、0,即左侧会有默认15像素的空白

全局设置每个cell的separatorInset值。在UITableViewController的-(void)viewDidLoad方法中设置UITableView分割线,UIEdgeInsetsZero相当于UIEdgeInsetsMake(0, 0, 0, 0)。

 -(void)viewDidLoad
{
//设置分割线距边界的距离
//在iOS7之前没有separatorInset,运行会导致程序崩溃,所以要加下判断
//if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
//{
// self.tableView.separatorInset = UIEdgeInsetsZero;
//}
if([self.tableView respondsToSelector:@selector(setSeparatorInset:)])
{
self.tableView.separatorInset = UIEdgeInsetsZero;
}
}

或者单独每个cell的separatorInset值。在UITableViewController的-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath方法中设置UITableView分割线,UIEdgeInsetsZero相当于UIEdgeInsetsMake(0, 0, 0, 0)。

 -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
//设置分割线距边界的距离
//在iOS7之前没有separatorInset,运行会导致程序崩溃,所以要加下判断
//if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
//{
// cell.separatorInset = UIEdgeInsetsZero;
//}
if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
cell.separatorInset = UIEdgeInsetsZero;
}
}

运行,发现达不到我想要的效果,分割线只是距离最左端近了,但还是没显示到最左端,效果图如下:

二、问题分析

iOS7,想要设置cell的分割线显示到最左端,只需要设置separatorInset的值为UIEdgeInsetsZero。

iOS8,简单设置separatorInset的值为UIEdgeInsetsZero的方法已经无效了。UIView的layoutMargins 默认为{8, 8, 8, 8}。

cell的preservesSuperviewLayoutMargins默认为true时,可能会导致cell被其父UITableView的LayoutMargin影响。如果设置为false时,cell不被UITableView的LayoutMargin影响。

三、问题解决

1.方法一:

全局设置cell的separatorInset的值为UIEdgeInsetsZero,UITableView的layoutMargins设置为UIEdgeInsetsZero,并且cell的layoutMargins设置为UIEdgeInsetsZero。

 -(void)viewDidLoad
{
//设置分割线距边界的距离
//在iOS7之前没有separatorInset,运行会导致程序崩溃,所以要加下判断
//if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
//{
// self.tableView.separatorInset = UIEdgeInsetsZero;
//}
if([self.tableView respondsToSelector:@selector(setSeparatorInset:)])
{
self.tableView.separatorInset = UIEdgeInsetsZero;
}
if([self.tableView respondsToSelector:@selector(setLayoutMargins:)])
{
self.tableView.layoutMargins = UIEdgeInsetsZero;
}
}
 -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
cell.layoutMargins = UIEdgeInsetsZero;
}
}

2.方法二:

preservesSuperviewLayoutMargins默认为true,cell被UITableView的layoutMargins影响。

设置cell的separatorInset值为UIEdgeInsetsZero,cell的layoutMargins值为UIEdgeInsetsZero,并且cell的preservesSuperviewLayoutMargins为false时,避免cell被UITableView的layoutMargins影响。

 -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
cell.separatorInset = UIEdgeInsetsZero;
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
cell.layoutMargins = UIEdgeInsetsZero;
}
//preservesSuperviewLayoutMargins设置为false时,子view不被其父view的LayoutMargin影响
if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
cell.preservesSuperviewLayoutMargins = false;
}
}

最终效果:

四、存在问题

该方法设置cell的分割线,在竖屏下显示正常,在横屏下会无效。网上查阅,发现解决cell分割线的方法都存在着这个问题

五、饮水思源

1.http://stackoverflow.com/questions/25770119/ios-8-uitableview-separator-inset-0-not-working

2.http://dev.classmethod.jp/smartphone/iphone/ios-8-uitableview-layoutmargins/

3.http://www.cnblogs.com/Alex-798-Dcr/p/5279920.html

4.http://www.skyfox.org/ios7-tableview-separatorinset-ajust.html

设置UITableView的separatorInset值为UIEdgeInsetsZero,分隔线不最左端显示的问题的更多相关文章

  1. iOS 中隐藏UITableView最后一条分隔线

    如何优雅的隐藏UITableView中最后一条分割线? 这个问题是很常见,却又不太容易解决的. 可能通常的做法都是隐藏UITableView的分割线,自定义一条. 最近在使用弹出菜单的时候,同样遇到了 ...

  2. 快速设置UITableView不同section对应于不同种类的cell

    快速设置UITableView不同section对应于不同种类的cell 本文主要是为了写明如何在UITableView中,一个section对应于一种类型的cell,写起来不凌乱. 在不封装任何类的 ...

  3. UITableView分隔线

    问题1: 在ios中使用UITableView时,当行数较少是,可能一屏幕能显示完全所有行,这时候会出现下面的问题,显示多余的分隔线 图如下: 解决方案: //解决方案1 //添加如下代码 -(CGF ...

  4. 设置easyui input默认值

    /*设置input 焦点*/ $(function () { //集体调用 $(".formTextBoxes input").each(function () { $(this) ...

  5. 给自定义cell设置分隔线的不同做法

    1.给cell添加一个UIView,设置UIView的高度为1,并设置这个UIView的左.下.右约束. 2.不需要给cell添加任何控件,重写cell的- (void)setFrame:(CGRec ...

  6. 设置UITableView背景透明/监听cell左边的删除按钮的点击事件

    _tableView = [[UITableView alloc] init]; _tableView.delegate = self; _tableView.dataSource = self; _ ...

  7. Linux 命令 - umask: 显示或设置文件模式掩码值

    umask 命令控制着创建文件时指定给文件的默认权限.它使用八进制表示法从文件模式属性中删除一个位掩码. 参见下面的例子: [huey@huey-K42JE cmdline]$ rm -f foo.t ...

  8. 定义设置颜色的RGB值的宏

    //定义设置颜色的RGB值的宏 #define RGBA(r,g,b,a) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha ...

  9. SharePoint 设置Lookup 字段的值

    如何设置Lookup字段的值, 首先我们同样需要了解SPFieldLookupValueCollection和SPFieldLookupValue, 这2个类的原理和之前所讲解到SPFieldUser ...

随机推荐

  1. 微信小程序之页面路由(九)

    [未经允许,请勿以任何形式转载] 什么是路由? 我们通常理解的路由指分组数据包从源到目的地时,决定端到端路径的网络范围的进程: 借用上面的定义,我们可以理解小程序页面路由,根据路由规则(路径)从一个页 ...

  2. web 前端常用组件【06】Upload 控件

    因为有万恶的IE存在,所以当Web项目初始化并进入开发阶段时. 如果是项目经理,需要知道客户将会用什么浏览器来访问系统. 明确知道限定浏览器的情况下,你才能从容的让手下的封装必要的前端组件. 本篇文章 ...

  3. 2.1 python使用MongoDB 示例代码

    import pymongo client = pymongo.MongoClient('localhost', 27017) # MongoDB 客户端 walden = client['walde ...

  4. WCF开发那些需要注意的坑 Z

    执行如下 批处理:"C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\svcutil.exe" http://127.0.0.1: ...

  5. Pycharm 输出中文或打印中文乱码现象的解决办法

    1. 确保文件开头加上以下代码: # -*- coding:utf-8 -*- 还可以加上 import sys reload(sys) sys.setdefaultencoding('utf-8') ...

  6. BZOJ 3223: Tyvj 1729 文艺平衡树

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3628  Solved: 2052[Submit][Sta ...

  7. phpexcel导入数据提示失败

    phpexcel导入excel时明明只有几行数据,却提示506行失败,原来是excel中有506行"无效数据"(看起来是空的,但是和没有数据不一样).

  8. 今天遇到sqlyog连接不上阿里云的数据库,最后百度解决了...

  9. Bash 小问题【待更新】

    bash 问题: 编写一个函数,用来返回某个目录下的目录个数.对于主目录下的所有目录,显示其属性信息,并把属性信息重定位到file_n(n=1.2.3)文件(第一个目录信息重定位到file_1, 第二 ...

  10. 关于跨域名的信息共享P3P实例

    首先我这里用到了redis 和 p3p技术.当然任意的nosql都可以满足 模拟的一个登陆访问的客户端. <?php session_start(); $get = $_GET; ') { $t ...