ios 分隔线前的空白进入我的 TableView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19499366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:28:14  来源:igfitidea点击:

White space before separator line into my TableView

iphoneiosuitableviewinterface-builder

提问by Safari

I have a question about UITableView... I have a UITableViewController and I created a custom cell. When I visualize the tableView I see a little white space before the separator line as you can see into this screenshot:

我有一个关于 UITableView 的问题......我有一个 UITableViewController 并且我创建了一个自定义单元格。当我将 tableView 可视化时,我会在分隔线之前看到一点空白,如您在此屏幕截图中所见:

enter image description here

在此处输入图片说明

Why? It is a default visualize? Can I change something to remove this white left padding?

为什么?它是默认的可视化?我可以改变一些东西来去除这个白色的左填充吗?

回答by Ashok

The leading whitespace is provided by default in iOS 7, even for custom cells.

iOS 7 中默认提供前导空格,即使对于自定义单元格也是如此。

Checkout this property separatorInsetof UITableviewCell to remove/add white spacing at either ends of cell's line separator.

检查separatorInsetUITableviewCell 的此属性以删除/添加单元格行分隔符两端的空白间距。

// Remove white space

// 去除空白

cell.separatorInset = UIEdgeInsetsZero;

Alternatively, at UITableView level, you can use this property -

或者,在 UITableView 级别,您可以使用此属性 -

if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {  // Safety check for below iOS 7 
    [tableView setSeparatorInset:UIEdgeInsetsZero];
}

Update - Below code works on iOS 7 and iOS 8:

更新 - 以下代码适用于 iOS 7 和 iOS 8:

-(void)viewDidLayoutSubviews
{
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }

    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }

    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

回答by michaelavila

Alternatively, you can also edit this in interface builder (IB):

或者,您也可以在界面构建器 (IB) 中编辑它:

  1. Go to IB.
  2. Select Table View.
  3. Open "Attribute Inspector" on the right.
  4. Change "Separator Insets" from Default to Custom.
  5. Change the "Left" attribute from 15 to 0.
  1. 去IB。
  2. 选择表视图。
  3. 打开右侧的“属性检查器”。
  4. 将“分隔符插入”从默认更改为自定义。
  5. 将“左”属性从 15 更改为 0。

This has the same effect as @Ashok's answer, but doesn't require writing any code.

这与@Ashok 的答案具有相同的效果,但不需要编写任何代码。

UpdateWorks on iOS 7 and 8

更新适用于 iOS 7 和 8

UpdateWorks on iOS 9

更新适用于 iOS 9

回答by Anooj VM

Use below code to remove the unwanted padding in UITableView. It works on both IOS 8 & 7

使用下面的代码删除 UITableView 中不需要的填充。它适用于 IOS 8 和 7

-(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];
    }
}

回答by David H

There is no white space! I entered a bug on this, Apple just closed it as "not a bug", but told me why its not a bug. I wrote a simple projectthat sets a color for every possible view in the app. What I see is that the color of those pixels is actually the background color of the cell itself (not the contentsView!), just as Apple had told me.

没有空白!我为此输入了一个错误,Apple 只是将其关闭为“不是错误”,但告诉我为什么它不是错误。我写了一个简单的项目,为应用程序中的每个可能的视图设置颜色。我看到的是,这些像素的颜色实际上是单元格本身的背景颜色(不是内容视图!),正如 Apple 告诉我的那样。

The cell.contentView.backgroundColor is green, and the cell.background color is red (taken with Pixie):

cell.contentView.backgroundColor 为绿色,cell.background 颜色为红色(用 Pixie 拍摄):

enter image description here

在此处输入图片说明

In the end, without a separator, the cell.contentView fills the cell completely. With a separator, there is a pixel or two gap at the bottom. The separator, when inset, fills the most of the gap, but there are then some pixels of the cell showing through.

最后,没有分隔符,cell.contentView 完全填充了单元格。使用分隔符,底部有一个像素或两个间隙。分隔符在插入时填充了大部分间隙,但仍有一些单元格的像素显示出来。

Mystery solved!

谜团已揭开!

EDIT: It seems that depending on how you configure your Storyboard, that the contentView.backgroundColor gets "lost" or set to White. If you over ride it when supplying cells you can get the behavior your want:

编辑:似乎取决于您如何配置故事板, contentView.backgroundColor 会“丢失”或设置为白色。如果您在提供单元格时超越它,您可以获得您想要的行为:

    let cell = tableView.dequeueReusableCellWithIdentifier("FOO", forIndexPath: indexPath) as UITableViewCell

    cell.backgroundColor = UIColor.redColor()
    cell.contentView.backgroundColor = UIColor.greenColor()

回答by Shanmugasundharam

Swift

迅速

cell.separatorInset = UIEdgeInsetsMake(0, 0, cell.frame.size.width, 0)
if (cell.respondsToSelector("preservesSuperviewLayoutMargins")){
        cell.layoutMargins = UIEdgeInsetsZero
        cell.preservesSuperviewLayoutMargins = false
   }

This one worked for me

这个对我有用

Thanks

谢谢

回答by lg365

This did the trick for me:

这对我有用:

cell?.layoutMargins = UIEdgeInsetsZero;
cell?.preservesSuperviewLayoutMargins = false

回答by ericgu

For those of you who want to make the retain the UIEdgeInsetSettings in Swift and are not using Storyboards:

对于那些想要在 Swift 中保留 UIEdgeInsetSettings 并且不使用 Storyboards 的人:

Before: (Default Behavior)

之前:(默认行为)

Before, Default Setting

之前,默认设置

Add the following code:

添加以下代码:

tableView.separatorInset.right = tableView.separatorInset.left

After, Implemented

之后,实施

回答by Naresh Reddy M

Here is an extension to UITableViewCell Class for swift 3 & later

这是 swift 3 及更高版本的 UITableViewCell 类的扩展

extension UITableViewCell
{
    func removeSeparatorLeftPadding() -> Void
    {
        if self.responds(to: #selector(setter: separatorInset)) // Safety check
        {
            self.separatorInset = UIEdgeInsets.zero
        }
        if self.responds(to: #selector(setter: layoutMargins)) // Safety check
        {
            self.layoutMargins = UIEdgeInsets.zero
        }
    }
}

Usage:

用法:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell : UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "menuCellID")!
    // .. Your other code
    cell.removeSeparatorLeftPadding()
    return cell
}

Hope this helps some one!

希望这对某人有所帮助!

Naresh.

纳雷什。