设置UITableView的separatorInset值为UIEdgeInsetsZero,分隔线不最左端显示的问题
一、问题描述
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,分隔线不最左端显示的问题的更多相关文章
- iOS 中隐藏UITableView最后一条分隔线
如何优雅的隐藏UITableView中最后一条分割线? 这个问题是很常见,却又不太容易解决的. 可能通常的做法都是隐藏UITableView的分割线,自定义一条. 最近在使用弹出菜单的时候,同样遇到了 ...
- 快速设置UITableView不同section对应于不同种类的cell
快速设置UITableView不同section对应于不同种类的cell 本文主要是为了写明如何在UITableView中,一个section对应于一种类型的cell,写起来不凌乱. 在不封装任何类的 ...
- UITableView分隔线
问题1: 在ios中使用UITableView时,当行数较少是,可能一屏幕能显示完全所有行,这时候会出现下面的问题,显示多余的分隔线 图如下: 解决方案: //解决方案1 //添加如下代码 -(CGF ...
- 设置easyui input默认值
/*设置input 焦点*/ $(function () { //集体调用 $(".formTextBoxes input").each(function () { $(this) ...
- 给自定义cell设置分隔线的不同做法
1.给cell添加一个UIView,设置UIView的高度为1,并设置这个UIView的左.下.右约束. 2.不需要给cell添加任何控件,重写cell的- (void)setFrame:(CGRec ...
- 设置UITableView背景透明/监听cell左边的删除按钮的点击事件
_tableView = [[UITableView alloc] init]; _tableView.delegate = self; _tableView.dataSource = self; _ ...
- Linux 命令 - umask: 显示或设置文件模式掩码值
umask 命令控制着创建文件时指定给文件的默认权限.它使用八进制表示法从文件模式属性中删除一个位掩码. 参见下面的例子: [huey@huey-K42JE cmdline]$ rm -f foo.t ...
- 定义设置颜色的RGB值的宏
//定义设置颜色的RGB值的宏 #define RGBA(r,g,b,a) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha ...
- SharePoint 设置Lookup 字段的值
如何设置Lookup字段的值, 首先我们同样需要了解SPFieldLookupValueCollection和SPFieldLookupValue, 这2个类的原理和之前所讲解到SPFieldUser ...
随机推荐
- EventToCommand
EventToCommand 在WPF中,并不是所有控件都有Command,例如TextBox,那么当文本改变,我们需要处理一些逻辑,这些逻辑在ViewModel 中,没有Command如何绑定呢?这 ...
- 20145233 GDB调试汇编分析
GDB调试汇编分析 代码 #include<stdio.h> short addend1 = 1; static int addend2 = 2; const static long ad ...
- 第二次团队作业——预则立&&他山之石
Deadline: 2016.10.16 22:00pm 一.确立团队选题,建立和初步熟悉团队git的协作方式.项目后续的代码.文档都要通过github增量式管理.实现文档的版本化和增量式管理. 二. ...
- ipvsadm命令
NAME ipvsadm - Linux Virtual Server administration SYNOPSIS ipvsadm -A|E -t|u|f service-address [-s ...
- BZOJ3331: [BeiJing2013]压力
传送门 Tarjan的三大应用之一:求解点双联通分量. 求解点双联通分量.然后缩点,差分优化即可. //BZOJ 3331 //by Cydiater //2016.10.29 #include &l ...
- easyUi load方法重新加载表单的数据
1.表单回显数据的方法 <script> //方法一 function loadLocal(){ $('#ff').form('load',{ name:'myname', email:' ...
- linux 乱码
xwindow终端乱码 在/etc/sysconfig/i18n文件的最后一行添加如下内容: export LC_ALL="zh_CN.GB18030" vi /etc/sysco ...
- Gravatar注册
今天在学习falsk时需要用到头像扩展,需要使用gravatar到网上查了查好像没有被墙,就试着用qq邮箱注册了一个账号,结果等了好长时间也没有等到激活邮箱,就百度了一下发现了一个技巧,原来这封邮件被 ...
- centos7删除自带openjdk
一些开发版的centos会自带jdk,我们一般用自己的jdk,把自带的删除.先看看有没有安装java -version [root@java-test-01 ~]# java -version ope ...
- sublime text3好用的插件
1.安装package control sublime text3 的安装方法,ctrl+`,调出控制台输入 import urllib.request,os; pf = 'Package Contr ...