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

时间:2015-07-01 19:53:39

标签: ios objective-c uitableview

我正在尝试删除一个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;
}

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

13 个答案:

答案 0 :(得分:23)

在iOS 8上,你需要使用:

cell.layoutMargins = UIEdgeInsetsZero

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

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

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

ADD:如果之前没有工作 - 请使用此功能。 (来自下面的答案)

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

答案 1 :(得分:16)

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

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

答案 2 :(得分:6)

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

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

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

答案 3 :(得分:3)

雨燕4

iOS 11

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

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

答案 4 :(得分:2)

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

override func awakeFromNib() {
    super.awakeFromNib()

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

答案 5 :(得分:2)

快速5享受

//MARK:- In which row you want to hide 
cell.separatorInset = UIEdgeInsets(top: 0, left: CGFloat.greatestFiniteMagnitude, bottom: 0, right: 0);

答案 6 :(得分:1)

对于那些为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)
    }
}

答案 7 :(得分:1)

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

答案 8 :(得分:0)

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

我使用label和separatorLineView

创建了SampleTableViewCell和一个nib文件
@interface SampleTableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIView *separatorLineView;
@end

然后,在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

答案 9 :(得分:0)

我创建了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
            }
        }
    }
}

答案 10 :(得分:0)

Swift 5.1-

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

答案 11 :(得分:0)

如果您只想显示可见单元格的分隔符,则可以尝试以下操作:

tableView.separatorInset = UIEdgeInsets(top: 0, left: 1000, bottom: 0, right: 0)

let visibleCell = tableView.visibleCells

    for cellV in visibleCell{
        cellV.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    }

答案 12 :(得分:-8)

Swift 3删除分隔符所有单元格:

 override func viewDidLoad() {
        self.yourTableView.separatorStyle = UITableViewCellSeparatorStyle.none
    }
相关问题