ios 仅删除一个单元格的分隔线

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

Remove separator line for only one cell

iosobjective-cuitableview

提问by Jessica

I'm trying to remove the separator for one UITableViewCell. I did the following:

我正在尝试删除 one 的分隔符UITableViewCell。我做了以下事情:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
    cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];

    if (indexPath.row == 1)
        cell.separatorInset = UIEdgeInsetsZero;

    return cell;
}

(It's static cells.) When I run the app. The separator line is still there. How can I remove the separator line for one cell?

(它是static cells。)当我运行应用程序时。分隔线仍然存在。如何删除一个分隔线cell

采纳答案by Grzegorz Krukowski

On iOS 8 you need to use:

在 iOS 8 上,您需要使用:

cell.layoutMargins = UIEdgeInsetsZero

If you want to be compatible with iOS 7 as well you should do following:

如果您还想与 iOS 7 兼容,您应该执行以下操作:

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

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

ADD:If previous didn't work - use this. (from answer below)

添加:如果以前不起作用 - 使用这个。(来自下面的回答)

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

If none of above worked, you can do:

如果以上都不起作用,您可以执行以下操作:

self.tableView.separatorColor = [UIColor clearColor];

self.tableView.separatorColor = [UIColor clearColor];

but this will leave 1 pixel empty space, not really removing a line, more making it transparent.

但这会留下 1 个像素的空白空间,而不是真正删除一条线,而是使其透明。

回答by Valentin Shergin

You can just visually hide separator through separatorInsetproperty:

您可以通过separatorInset属性直观地隐藏分隔符:

tableViewCell.separatorInset = UIEdgeInsets(top: 0, left: 10000, bottom: 0, right: 0)

回答by dimpiax

Swift 4

斯威夫特 4

iOS 11

iOS 11

Assign next values for specific cell you need for customization.

为自定义所需的特定单元格分配下一个值。

cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, .greatestFiniteMagnitude)
cell.directionalLayoutMargins = .zero

回答by Kostas Tsoleridis

If you want to hide the separation line for only a specific type of cell, you can use the following code:

如果只想隐藏特定类型单元格的分隔线,可以使用以下代码:

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

Write this code in the cell (it must be a subclass of UITableViewCell) for which you do not want a separation line to appear.

UITableViewCell不希望出现分隔线的单元格(它必须是 的子类)中编写此代码。

回答by COzkurt

Just add following to the cell you don't want separator (swift 3) :

只需将以下内容添加到您不需要分隔符的单元格中(swift 3):

override func awakeFromNib() {
    super.awakeFromNib()

    // remove separator
    self.separatorInset = UIEdgeInsetsMake(0, 1000, 0, 0)
}

回答by katwal-Dipak

cell.separatorInset = UIEdgeInsetsMake(0.0 , cell.bounds.size.width , 0.0, -cell.bounds.size.width)

回答by boidkan

For those people struggling with this for iOS 9 the solution is a little different than the answers already given. Here is how I solved it in Swift:

对于那些为 iOS 9 苦苦挣扎的人来说,解决方案与已经给出的答案略有不同。这是我在 Swift 中解决它的方法:

override func viewWillAppear(animated: Bool) {
    table.cellLayoutMarginsFollowReadableWidth = false
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if IS_THE_CELL_WITH_NO_SEPARATOR{
        cell.separatorInset = UIEdgeInsetsZero
        cell.preservesSuperviewLayoutMargins = false
        cell.layoutMargins = UIEdgeInsetsZero
    }
}

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    if IS_THE_CELL_WITH_NO_SEPARATOR{
        let size = self.view.bounds.size
        let rightInset = size.width > size.height ? size.width : size.height
        cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, rightInset)
    }
}

回答by thanyaj

Another way that is a bit hacky is to create the custom table view cell with a uiView that acts like separator inset. Then, hide and show that when you want to.

另一种有点hacky的方法是使用uiView创建自定义表格视图单元格,其作用类似于分隔符插入。然后,在需要时隐藏并显示。

I created SampleTableViewCell and a nib file with label and separatorLineView

我创建了 SampleTableViewCell 和一个带有标签和 separatorLineView 的 nib 文件

@interface SampleTableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIView *separatorLineView;
@end

Then, in ViewController Class

然后,在 ViewController 类中

@interface ViewController () 
@property (nonatomic) NSArray *items;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.items = @[@"A", @"B", @"C"];
    [self.tableView registerNib:[UINib nibWithNibName:@"SampleTableViewCell" bundle:nil] forCellReuseIdentifier:@"SampleCell"];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SampleTableViewCell *cell = (SampleTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"SampleCell" forIndexPath:indexPath];
    cell.titleLabel.text = self.items[indexPath.row];
    if (indexPath.row == 1) {
        cell.separatorLineView.hidden = YES;
    } else {
        cell.separatorLineView.hidden = NO;
    }
    return cell;
}

@end

回答by Narlei Moreira

I created a Extension to UITableViewCell, setting separatorInset value brings anchor bugs to me, I'm using Eureka form Pod.

我为 UITableViewCell 创建了一个扩展,设置 separatorInset 值给我带来了锚点错误,我正在使用 Eureka 表单 Pod。

extension UITableViewCell {
    func hideSeparator(hide: Bool) {
        let subviews = self.subviews
        for view in subviews {
            if view.classForCoder.description() == "_UITableViewCellSeparatorView" {
                view.isHidden = hide
            }
        }
    }
}

回答by Yash

Swift 5.1 -

斯威夫特 5.1 -

override public func layoutSubviews() {
    super.layoutSubviews()
    subviews.forEach { (view) in
        if type(of: view).description() == "_UITableViewCellSeparatorView" {
            view.isHidden = true
        }
    }
}