iOS9 UITableViewCell 分割线左顶头
来源:互联网 发布:支持windows的平板 编辑:程序博客网 时间:2024/04/29 17:05
UITableViewCell在IOS6下分割线是默认顶头的,在IOS7以后左边会有一定的间距(15)。
网上有一些解决办法,分享我找到的最简单的一种:在数据源方法tableView:cellForRowAtIndexPath:
中加入如下三行
cell.preservesSuperviewLayoutMargins = NO; cell.separatorInset = UIEdgeInsetsZero; cell.layoutMargins = UIEdgeInsetsZero;
需要注意的是preservesSuperviewLayoutMargins
和layoutMargins
是iOS8以后才有的,separatorInset
是iOS7以后才有的,如果要兼容以前的版本记得先判断selector
是否存在。
至于原因如下:
Setting the separatorInset of tableView to UIEdgeInsetsZero is not going to work. It will only effect the extra separators, not the cells that you created.
iOS8 introduces layoutMargins property on UIView, along with another property called preservesSuperviewLayoutMargins indicating whether superview’s layoutMargins is preserved (Think of it as an override) which is YES by default. layoutMargins property is a set of insets from the edge of the view’s bounds that denote a default spacing for laying out content.
The default separatorInset of tableView and tableViewCell is (top = 0, left = 15, bottom = 0, right = 0)
The default layoutMargins of tableView and tableViewCell is (top = 8, left = 8, bottom = 8, right = 8)
These 3 properties are defining the behaviour of separator indentation.
However, setting tableView’s layoutMargins is not going to effect tableViewCell. Because tableViewCell’s superview is not tableView. It’s tableView’s subView, an instance of UITableViewWrapperView whose superClass is UIScrollView and whose layoutMargins is (top = 8, left = 15, bottom = 8, right = 15) and preservesSuperviewLayoutMargins is YES.
You have to set separatorInset and layoutMargins of tableViewCell directly:
tableViewCell.preservesSuperviewLayoutMargins = NO;
tableViewCell.separatorInset = UIEdgeInsetsZero;
tableViewCell.layoutMargins = UIEdgeInsetsZero;
You can put those code in cell’s init method or awakeFromNib or tableView’s delegate tableView:willDisplayCell:fromRowAtIndexPath, etc.
大致的意思是说iOS7只有separatorInset
的概念,在IOS7下tableView
的分割线左边有15像素的间距。
iOS8中还有layoutMargins
,用来表示子view和父view的间距,有8像素的间距。preservesSuperviewLayoutMargins
默认为true
表示开启子view与父view的间距。
References:
《iOS8 UITableView 分割线顶头》
《iOS7 tableview separatorInset cell分割线左对齐》
- iOS9 UITableViewCell 分割线左顶头
- UITableViewCell 分割线顶头
- UItableViewCell的分割线顶头显示
- UITableViewCell 分割线左对齐
- tableView 分割线顶头
- iOS 8 UITableViewCell 分割线 左对齐
- 去除UITableViewCell分割线的左间隙
- tableView分割线顶头效果
- tableView cell 分割线顶头
- iOS UITableViewCell 分割线不到左部顶端的问题!
- 解决tableView分割线不顶头
- UITableViewCell自定义分割线
- UITableViewCell自定义分割线
- UITableViewCell 分割线顶格
- UITableViewCell的分割线
- UITableView 去掉多余行数、Cell系统分割线顶头
- iOS -- UITableViewCell的分割线
- uitableviewcell去掉多余分割线
- 【数据结构】哈希表的线性探测算法
- 【数据结构】处理哈希冲突的开链法(哈希桶)算法实现
- 【C语言】输入一个整数,输出该数二进制表示中1的个数(三种方法)
- 【C语言】单链表的相关热点面试题(包括:从尾到头打印,逆置,冒泡,寻找中间节点,倒数k节点)
- 【数据结构】位图BitMap、布隆过滤器的算法实现
- iOS9 UITableViewCell 分割线左顶头
- 【数据结构】两个队列实现一个栈
- DB Version Control Tool "Flyway" note 2 Auto run.
- 【数据结构】常用比较排序算法(包括:选择排序,堆排序,冒泡排序,选择排序,快速排序,归并排序)
- 【Linux】Linux系统下find指令详细用法与示例
- 【数据结构】大量数据(20万)的快速排序的递归与非递归算法、三数取中思想
- 12 种编程语言的起源故事
- 【数据结构】非比较排序的算法实现(包括计数排序、计数排序)
- 【Linux】LINUX操作系统下VIM的安装和配置