我有一个应用程序,其中UITableView的分隔符插入设置为自定义值-右0,左0。这在iOS 7中非常有效。x,但是在iOS 8.0中,我看到右边的分隔符被设置为默认值15。即使在xib文件中它设置为0,它仍然不正确地显示。
如何删除UITableViewCell分隔符边距?
我有一个应用程序,其中UITableView的分隔符插入设置为自定义值-右0,左0。这在iOS 7中非常有效。x,但是在iOS 8.0中,我看到右边的分隔符被设置为默认值15。即使在xib文件中它设置为0,它仍然不正确地显示。
如何删除UITableViewCell分隔符边距?
当前回答
这在我的iOS 8和iOS 9系统中运行得非常好。
对于OBJ-C
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([tableView respondsToSelector:@selector(setSeparatorInset:)])
{
[tableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([tableView respondsToSelector:@selector(setLayoutMargins:)])
{
[tableView setLayoutMargins:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)])
{
[cell setLayoutMargins:UIEdgeInsetsZero];
}
return cell;
}
其他回答
我相信这是我在这里问的相同的问题:为XCode 6 iPhone模拟器删除iOS 8 UITableView上的SeparatorInset
在iOS 8中,所有继承自UIView的对象都有一个新属性。在ios7中设置SeparatorInset的解决方案。x将不能删除你在iOS 8中UITableView上看到的空白。
新的属性叫做“layoutmargin”。
@property(nonatomic) UIEdgeInsets layoutMargins
Description The default spacing to use when laying out content in the view.
Availability iOS (8.0 and later)
Declared In UIView.h
Reference UIView Class Reference
解决方案:
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[tableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
[tableView setLayoutMargins:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
如果你设置单元格。layoutmargin = UIEdgeInsetsZero;如果不检查layoutmargin是否存在,应用程序将在iOS 7.x上崩溃。所以,最好的方法是在setlayoutmargin:UIEdgeInsetsZero之前检查layoutmargin是否存在。
iOS在单元格和表视图上引入了layoutmargin属性。
这个属性在iOS 7.0中不可用,所以你需要确保在分配它之前检查!
然而,苹果已经添加了一个名为preservessuperviewlayoutmargin的属性到你的单元格,这将阻止它继承你的表视图的边距设置。这样,单元格可以独立于表视图配置它们自己的边距。把它看作是一个重写。
这个属性叫做preservessuperviewlayoutmargin,将它设置为NO可以让你用你自己单元格的layoutMargin设置来覆盖你的表格视图的layoutMargin设置。它既节省时间(你不必修改表视图的设置),也更简洁。详情请参考Mike Abdullah的回答。
注意:这是正确的,较少混乱的实现,如Mike Abdullah的回答所示;设置你的单元格的preservessuperviewlayoutmargin =NO将确保你的表格视图不会覆盖单元格设置。
第一步-设置单元格边距:
/*
Tells the delegate that the table view is about to draw a cell for a particular row.
*/
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,
forRowAtIndexPath indexPath: NSIndexPath)
{
// Remove separator inset
if cell.respondsToSelector("setSeparatorInset:") {
cell.separatorInset = UIEdgeInsetsZero
}
// Prevent the cell from inheriting the Table View's margin settings
if cell.respondsToSelector("setPreservesSuperviewLayoutMargins:") {
cell.preservesSuperviewLayoutMargins = false
}
// Explictly set your cell's layout margins
if cell.respondsToSelector("setLayoutMargins:") {
cell.layoutMargins = UIEdgeInsetsZero
}
}
将单元格上的preservessuperviewlayoutmargin属性设置为NO可以防止表视图覆盖单元格边缘。在某些情况下,它似乎不能正常工作。
第二步——只有在所有失败的情况下,你才可以强制使用你的表视图边距:
/*
Called to notify the view controller that its view has just laid out its subviews.
*/
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
// Force your tableview margins (this may be a bad idea)
if self.tableView.respondsToSelector("setSeparatorInset:") {
self.tableView.separatorInset = UIEdgeInsetsZero
}
if self.tableView.respondsToSelector("setLayoutMargins:") {
self.tableView.layoutMargins = UIEdgeInsetsZero
}
}
...好了!这应该可以在iOS 8和iOS 7上运行。
注意:使用iOS 8.1和7.1进行测试,在我的情况下,我只需要使用本解释的第一步。
第二步只需要在渲染的单元格下面有未填充的单元格。如果表的行数大于表模型中的行数。不执行第二步将导致分隔符偏移量不同。
这是在Swift中为我工作的代码:
override func viewDidLoad()
{
super.viewDidLoad()
...
if tableView.respondsToSelector("setSeparatorInset:") {
tableView.separatorInset = UIEdgeInsetsZero
}
}
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,forRowAtIndexPath indexPath: NSIndexPath)
{
if cell.respondsToSelector("setSeparatorInset:") {
cell.separatorInset.left = CGFloat(0.0)
}
if tableView.respondsToSelector("setLayoutMargins:") {
tableView.layoutMargins = UIEdgeInsetsZero
}
if cell.respondsToSelector("setLayoutMargins:") {
cell.layoutMargins.left = CGFloat(0.0)
}
}
这对我来说似乎是最干净的(目前),因为所有的单元格/tableView边缘/边缘调整都是在tableView:willDisplayCell:forRowAtIndexPath:方法中完成的,而不是在tableView:cellForRowAtIndexPath:中填充不必要的代码。
顺便说一句,我只是设置单元格的左separatorInset/ layoutmargin,因为在这种情况下,我不想搞砸我在单元格中设置的约束。
代码更新到Swift 2.2:
override func viewDidLoad() {
super.viewDidLoad()
if tableView.respondsToSelector(Selector("setSeparatorInset:")) {
tableView.separatorInset = UIEdgeInsetsZero
}
}
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,forRowAtIndexPath indexPath: NSIndexPath) {
if cell.respondsToSelector(Selector("setSeparatorInset:")) {
cell.separatorInset.left = CGFloat(0.0)
}
if tableView.respondsToSelector(Selector("setLayoutMargins:")) {
tableView.layoutMargins = UIEdgeInsetsZero
}
if cell.respondsToSelector(Selector("setLayoutMargins:")) {
cell.layoutMargins.left = CGFloat(0.0)
}
}
这里有一个简单的方法全局删除嵌入。
在UITableViewCell +移到swift:
import UIKit
extension UITableViewCell {
override public var layoutMargins: UIEdgeInsets {
get { return UIEdgeInsetsZero }
set { }
}
}
在AppDelegate应用中:didFinishLaunchingWithOptions
UITableViewCell.appearance().separatorInset = UIEdgeInsetsZero
你可能会想a)也只是覆盖separatorInset在扩展,或b)设置外观代理的layoutmargin,而不是。这两种方法都行不通。即使separatorInset被指示为属性,尝试将其作为属性(或方法)重写也会产生编译器错误。并且为UITableViewCell的layoutmargin设置外观代理(或者,也为UITableView的layoutmargin和separatorInset设置外观代理)没有效果。
Lukasz用Swift回答:
// iOS 7:
UITableView.appearance().separatorStyle = .SingleLine
UITableView.appearance().separatorInset = UIEdgeInsetsZero
UITableViewCell.appearance().separatorInset = UIEdgeInsetsZero
// iOS 8:
if UITableView.instancesRespondToSelector("setLayoutMargins:") {
UITableView.appearance().layoutMargins = UIEdgeInsetsZero
UITableViewCell.appearance().layoutMargins = UIEdgeInsetsZero
UITableViewCell.appearance().preservesSuperviewLayoutMargins = false
}