设置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 ...
随机推荐
- RapidJSON 代码剖析(二):使用 SSE4.2 优化字符串扫描
现在的 CPU 都提供了单指令流多数据流(single instruction multiple data, SIMD)指令集.最常见的是用于大量的浮点数计算,但其实也可以用在文字处理方面. 其中,S ...
- 你真的理解Java的按引用传递吗?
首先我们来看下面这段代码: public class Test1 { String a = "123"; public static void change(Test1 test) ...
- jsoup-处理html中的script数据
/** * 价值在线数据-左边分类抓取 * http://www.valueonline.cn/laws/laws?typeid=96219074211635284 * @author hwaggLe ...
- Code[VS] 3123 高精度练习之超大整数乘法
FFT 做 高精度乘法 #include <bits/stdc++.h> ); struct complex { double a, b; inline complex( , ) { a ...
- 使用 zssh 进行 Zmodem 文件传输
Zmodem 最早是设计用来在串行连接(uart.rs232.rs485)上进行数据传输的,比如,在 minicom 下,我们就可以方便的用 Zmodem (说 sz .rz 可能大家更熟悉)传输文件 ...
- TP-LINK WR941 DD-WRT刷回OpenWRT及OpenWRT刷回原厂固件
1.DD-Wrt 刷回 OpenWrt A.从官网下载固件: root@TL-DDWRT:/tmp# wget http://downloads.openwrt.org/barrier_breaker ...
- ppt2013技术整理
1. 显示选择窗格 便于选择该页的所有元素.分组.隐藏与显示等. 位于:开始-编辑-选择-选择窗格 2. 显示动画窗格 便于调节页面中元素的动画状态. 位于:动画-高级动画-动画窗格 3. 绑定动画触 ...
- asp.net读取execl模板并填充数据,关闭进程
<head runat="server"> <title></title> <script src="Scripts/jquer ...
- struts2+hibernate 项目实战:图书管理系统
经典项目,练手必备. 图书管理系统 需求分析(大致,并不专业):1.需要有用户管理: 1.1 用户注册: 1.2 用户登录: 1.3 用户信息修改: 1.4 用户修改密码: 2.需要有书本管理: 2. ...
- win7电脑怎么修改计算机用户名Administrator
----------------------------------- 首先,在开始中打开我的控制面板.----->>打开用户账户和家庭安全选项.----->>,继续点击用户账 ...