一、问题描述

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. Mongodb学习笔记四(Mongodb聚合函数)

    第四章 Mongodb聚合函数 插入 测试数据 ;j<;j++){ for(var i=1;i<3;i++){ var person={ Name:"jack"+i, ...

  2. 通过COOKIE欺骗登录网站后台

    1.今天闲着没事看了看关于XSS(跨站脚本攻击)和CSRF(跨站请求伪造)的知识,xss表示Cross Site Scripting(跨站脚本攻击),它与SQL注入攻击类似,SQL注入攻击中以SQL语 ...

  3. SQL数据库分配权限

    打开SQL-Server管理工具->安全性->登陆名->右键(新建登陆名) 输入相应的信息(这里要去掉强制实施密码策略,强制密码过期,否则用户在下次登录时必须修改密码) 用户映射-& ...

  4. java中hashcode()和equals()的详解

    今天下午研究了半天hashcode()和equals()方法,终于有了一点点的明白,写下来与大家分享(zhaoxudong 2008.10.23晚21.36). 1. 首先equals()和hashc ...

  5. Microsoft.AspNet.Identity 自定义使用现有的表—登录实现

    Microsoft.AspNet.Identity是微软新引入的一种membership框架,也是微软Owin标准的一个实现.Microsoft.AspNet.Identity.EntityFrame ...

  6. 【Beta】Scrum04

    Info 由于上次验收基本没有人按时完成,缓冲一个任务周期. 时间:2016.12.06 21:30 时长:25min 地点:大运村1号公寓5楼楼道 类型:日常Scrum会议 NXT:2016.12. ...

  7. HTML5本地存储——IndexedDB(一:基本使用)

    在HTML5本地存储——Web SQL Database提到过Web SQL Database实际上已经被废弃,而HTML5的支持的本地存储实际上变成了 Web Storage(Local Stora ...

  8. vue2.0学习(二)

    1.关于模板渲染,当需要渲染多个元素时可以 <ul> <template v-for="item in items"> <li>{{ item. ...

  9. Javascript实现图片预加载【回调函数,多张图片】

    使用JS实现一组图片动画效果或者使用HTML5 Canvas渲染一系列图片等案例中,需要图片全部加载完成方可运行动画效果.此时程序中就会涉及多张图片预加载代码.当接二连三的案例中都涉及图片预加载时,就 ...

  10. SpringMVC Controller介绍

    SpringMVC Controller 介绍 一.简介 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理 ...