ios 在一个 UITableViewCell 上隐藏分隔线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8561774/
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-30 15:56:29  来源:igfitidea点击:

Hide separator line on one UITableViewCell

iosobjective-cuitableviewcocoa-touchseparator

提问by Safari

I'm customizing a UITableView. I want to hide the line separating on the lastcell ... can i do this?

我正在定制一个UITableView. 我想隐藏最后一个单元格上的分隔线……我可以这样做吗?

I know I can do tableView.separatorStyle = UITableViewCellStyle.Nonebut that would affect allthe cells of the tableView. I want it to only affect my last cell.

我知道我可以,tableView.separatorStyle = UITableViewCellStyle.None但这会影响tableView 的所有单元格。我希望它只影响我的最后一个单元格。

回答by Hiren

in viewDidLoad, add this line:

viewDidLoad,添加这一行:

self.tableView.separatorColor = [UIColor clearColor];

and in cellForRowAtIndexPath:

并在cellForRowAtIndexPath

for iOS lower versions

适用于 iOS 较低版本

if(indexPath.row != self.newCarArray.count-1){
    UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(0, 44, 320, 2)];
    line.backgroundColor = [UIColor redColor];
    [cell addSubview:line];
}

for iOS 7 upper versions (including iOS 8)

适用于 iOS 7 上版本(包括 iOS 8)

if (indexPath.row == self.newCarArray.count-1) {
    cell.separatorInset = UIEdgeInsetsMake(0.f, cell.bounds.size.width, 0.f, 0.f);
}

回答by Avinash

You can use the following code:

您可以使用以下代码:

Swift :

斯威夫特:

if indexPath.row == {your row number} {
    cell.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: .greatestFiniteMagnitude)
}

or :

或者 :

cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, UIScreen.main.bounds.width)

for default Margin:

默认保证金:

cell.separatorInset = UIEdgeInsetsMake(0, tCell.layoutMargins.left, 0, 0)

to show separator end-to-end

以端到端的方式显示分隔符

cell.separatorInset = .zero

Objective-C:

目标-C:

if (indexPath.row == {your row number}) {
    cell.separatorInset = UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, CGFLOAT_MAX);
}

回答by Benjamin

To follow up on Hiren's answer.

跟进Hiren的回答。

in ViewDidLoadand the following line :

ViewDidLoad和以下行中:

self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

Or, if you are using XIB's or Storyboards change "separator" to "none" :

或者,如果您使用 XIB 或 Storyboard,请将“分隔符”更改为“”:

Interface builder

界面生成器

And in CellForRowAtIndexPathadd this :

CellForRowAtIndexPath 中添加:

CGFloat separatorInset; // Separator x position 
CGFloat separatorHeight; 
CGFloat separatorWidth; 
CGFloat separatorY; 
UIImageView *separator;
UIColor *separatorBGColor;

separatorY      = cell.frame.size.height;
separatorHeight = (1.0 / [UIScreen mainScreen].scale);  // This assures you to have a 1px line height whatever the screen resolution
separatorWidth  = cell.frame.size.width;
separatorInset  = 15.0f;
separatorBGColor  = [UIColor colorWithRed: 204.0/255.0 green: 204.0/255.0 blue: 204.0/255.0 alpha:1.0];

separator = [[UIImageView alloc] initWithFrame:CGRectMake(separatorInset, separatorY, separatorWidth,separatorHeight)];
separator.backgroundColor = separatorBGColor;
[cell addSubView: separator];

Here is an example of the result where I display a tableview with dynamic Cells (but only have a single one with contents). The result being that only that one has a separator and not all the "dummy" ones tableview automatically adds to fill the screen.

这是一个结果示例,其中我显示了一个带有动态单元格的表格视图(但只有一个包含内容的表格视图)。结果是只有那个有一个分隔符,而不是所有的“虚拟”tableview 会自动添加以填充屏幕。

enter image description here

enter image description here

Hope this helps.

希望这可以帮助。

EDIT:For those who don't always read the comments, there actually is a better way to do it with a few lines of code :

编辑:对于那些不总是阅读评论的人,实际上有一种更好的方法可以用几行代码来完成:

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.tableFooterView = UIView()
}

回答by Kemenaran

If you don't want to draw the separator yourself, use this:

如果您不想自己绘制分隔符,请使用以下命令:

  // Hide the cell separator by moving it to the far right
  cell.separatorInset = UIEdgeInsetsMake(0, 10000, 0, 0);

This API is only available starting from iOS 7 though.

不过,此 API 仅从 iOS 7 开始可用。

回答by crifan

my develop environment is

我的开发环境是

  • Xcode 7.0
  • 7A220 Swift 2.0
  • iOS 9.0
  • Xcode 7.0
  • 7A220 雨燕 2.0
  • iOS 9.0

above answers not fully work for me

以上答案并不完全适合我

after try, my finally working solution is:

经过尝试,我最后的工作解决方案是:

let indent_large_enought_to_hidden:CGFloat = 10000
cell.separatorInset = UIEdgeInsetsMake(0, indent_large_enought_to_hidden, 0, 0) // indent large engough for separator(including cell' content) to hidden separator
cell.indentationWidth = indent_large_enought_to_hidden * -1 // adjust the cell's content to show normally
cell.indentationLevel = 1 // must add this, otherwise default is 0, now actual indentation = indentationWidth * indentationLevel = 10000 * 1 = -10000

and the effect is: enter image description here

效果是: enter image description here

回答by Matthew Korporaal

Set separatorInset.right = .greatestFiniteMagnitudeon your cell.

设置separatorInset.right = .greatestFiniteMagnitude在您的手机上。

回答by Okhan Okbay

In Swift 3, Swift 4 and Swift 5, you can write an extension to UITableViewCell like this:

Swift 3、Swift 4 和 Swift 5 中,您可以像这样编写 UITableViewCell 的扩展:

extension UITableViewCell {
  func separator(hide: Bool) {
    separatorInset.left = hide ? bounds.size.width : 0
  }
}

Then you can use this as below (when cell is your cell instance):

然后你可以使用它如下(当单元格是你的单元格实例时):

cell.separator(hide: false) // Shows separator 
cell.separator(hide: true) // Hides separator

It is really better assigning the width of table view cell as left inset instead of assigning it some random number. Because in some screen dimensions, maybe not now but in future your separators can still be visible because that random number may not be enough. Also, in iPad in landscape mode you can't guarantee that your separators will always be invisible.

将表格视图单元格的宽度分配为左插入而不是分配一些随机数确实更好。因为在某些屏幕尺寸中,可能现在不是,但将来您的分隔符仍然可见,因为该随机数可能还不够。此外,在横屏模式下的 iPad 中,您无法保证分隔符始终不可见。

回答by Sound Blaster

Better solution for iOS 7 & 8

更好的 iOS 7 & 8 解决方案

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    DLog(@"");
    if (cell && indexPath.row == 0 && indexPath.section == 0) {

        DLog(@"cell.bounds.size.width %f", cell.bounds.size.width);
        cell.separatorInset = UIEdgeInsetsMake(0.f, cell.bounds.size.width, 0.f, 0.0f);
    }
}

If your app is rotatable — use 3000.0f for left inset constant or calc it on the fly. If you try to set right inset you have visible part of separator on the left side of cell on iOS 8.

如果您的应用程序是可旋转的 - 使用 3000.0f 作为左插入常量或即时计算它。如果您尝试设置右侧插图,则 iOS 8 单元格左侧的分隔符部分可见。

回答by Enrico Susatyo

In iOS 7, the UITableView grouped style cell separator looks a bit different. It looks a bit like this:

在 iOS 7 中,UITableView 分组样式单元格分隔符看起来有点不同。它看起来有点像这样:

enter image description here

enter image description here

I tried Kemenaran's answerof doing this:

我尝试了 Kemenaran 的回答

cell.separatorInset = UIEdgeInsetsMake(0, 10000, 0, 0);

However that doesn't seem to work for me. I'm not sure why. So I decided to use Hiren's answer, but using UIViewinstead of UIImageView, and draws the line in the iOS 7 style:

然而,这似乎对我不起作用。我不知道为什么。所以我决定使用 Hiren's answer,但使用UIView而不是UIImageView,并在 iOS 7 风格中画线:

UIColor iOS7LineColor = [UIColor colorWithRed:0.82f green:0.82f blue:0.82f alpha:1.0f];

//First cell in a section
if (indexPath.row == 0) {

    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 1)];
    line.backgroundColor = iOS7LineColor;
    [cell addSubview:line];
    [cell bringSubviewToFront:line];

} else if (indexPath.row == [self.tableViewCellSubtitles count] - 1) {

    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(21, 0, self.view.frame.size.width, 1)];
    line.backgroundColor = iOS7LineColor;
    [cell addSubview:line];
    [cell bringSubviewToFront:line];

    UIView *lineBottom = [[UIView alloc] initWithFrame:CGRectMake(0, 43, self.view.frame.size.width, 1)];
    lineBottom.backgroundColor = iOS7LineColor;
    [cell addSubview:lineBottom];
    [cell bringSubviewToFront:lineBottom];

} else {

    //Last cell in the table view
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(21, 0, self.view.frame.size.width, 1)];
    line.backgroundColor = iOS7LineColor;
    [cell addSubview:line];
    [cell bringSubviewToFront:line];
}

If you use this, make sure you plug in the correct table view height in the second if statement. I hope this is useful for someone.

如果使用它,请确保在第二个 if 语句中插入正确的表格视图高度。我希望这对某人有用。

回答by Tualatrix Chou

In your UITableViewCell subclass, override layoutSubviews and hide the _UITableViewCellSeparatorView. Works under iOS 10.

在您的 UITableViewCell 子类中,覆盖 layoutSubviews 并隐藏 _UITableViewCellSeparatorView。在 iOS 10 下工作。

override func layoutSubviews() {
    super.layoutSubviews()

    subviews.forEach { (view) in
        if view.dynamicType.description() == "_UITableViewCellSeparatorView" {
            view.hidden = true
        }
    }
}