我在定制一个UITableView。我想隐藏在最后一个单元格上的分离线…我能这样做吗?

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


当前回答

我能找到的唯一解决办法就是下一个

extension UITableViewCell {
    func separator(hide: Bool) {
        separatorInset.left = hide ? self.bounds.width * 1.5 : 16 // or whatever you want to be the left inset
    }
}

我不知道为什么,但是self.bounds.width没有像预期的那样工作,所以我乘以1.5。

如果你想隐藏一个单元格,你只需要这样做:

cell.separator(hide: true)

对于其余的单元格,只发送参数为false

cell.separator(hide: false)

其他回答

正如(许多)其他人指出的那样,你可以通过简单地关闭整个UITableView本身来轻松隐藏所有UITableViewCell分隔符;例如在你的UITableViewController中

- (void)viewDidLoad {
    ...
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    ...
}

不幸的是,在每个单元格的基础上,这是一个真正的PITA,这是你真正要求的。

就我个人而言,我已经尝试了许多改变cell.separatorInset的排列。正如(许多)其他人所建议的那样,再次向左,但问题是,引用苹果的话(强调):

"...您可以使用此属性在当前单元格内容与表的左右边缘之间添加空格。正插入值将单元格内容和单元格分隔符向内移动,远离表格边缘……”

因此,如果你试图通过将分隔符推到屏幕的右侧来“隐藏”分隔符,你最终也会缩进你的单元格的contentView。正如crifan所建议的,您可以尝试通过设置cell来补偿这种讨厌的副作用。indentationWidth和cell。indentationLevel适当地将所有内容移回,但我发现这也是不可靠的(内容仍然缩进…)

我发现的最可靠的方法是在一个简单的UITableViewCell子类中覆盖layoutSubviews,并设置右插入,使它命中左插入,使分隔符有0宽度,因此不可见[这需要在layoutSubviews中自动处理旋转]。我还向我的子类添加了一个方便的方法来打开它。

@interface MyTableViewCellSubclass()
@property BOOL separatorIsHidden;
@end

@implementation MyTableViewCellSubclass

- (void)hideSeparator
{
    _separatorIsHidden = YES;
}

- (void)layoutSubviews
{
    [super layoutSubviews];

    if (_separatorIsHidden) {
        UIEdgeInsets inset = self.separatorInset;
        inset.right = self.bounds.size.width - inset.left;
        self.separatorInset = inset;
    }
}

@end

警告:没有一个可靠的方法来恢复原始的右插入,所以你不能“取消隐藏”分隔符,因此我为什么要使用一个不可逆的hideSeparator方法(vs暴露separatorIsHidden)。请注意,separatorInset在重用的单元格之间持续存在,因为你不能“取消隐藏”,你需要将这些隐藏的分隔单元格隔离在它们自己的reuseIdentifier中。

在使用iOS 8.4的Swift中:

/*
    Tells the delegate that the table view is about to draw a cell for a particular row. (optional)
*/
override func tableView(tableView: UITableView,
                        willDisplayCell cell: UITableViewCell,
                        forRowAtIndexPath indexPath: NSIndexPath)
{
    if indexPath.row == 3 {
        // Hiding separator line for only one specific UITableViewCell
        cell.separatorInset = UIEdgeInsetsMake(0, cell.bounds.size.width, 0, 0)
    }
}

Note: this snippet above will work on UITableView using dynamic cells. The only problem that you can encounter is when you use static cells with categories, a separator type different than none and a grouped style for the table view. In fact, in this particular case it will not hide the last cell of each category. For overcoming that, the solution that I found was to set the cell separator (through IB) to none and then creating and adding manually (through code) your line view to each cell. For an example, please check the snippet below:

/*
Tells the delegate that the table view is about to draw a cell for a particular row. (optional)
*/
override func tableView(tableView: UITableView,
    willDisplayCell cell: UITableViewCell,
    forRowAtIndexPath indexPath: NSIndexPath)
{
    // Row 2 at Section 2
    if indexPath.row == 1 && indexPath.section == 1 {
        // Hiding separator line for one specific UITableViewCell
        cell.separatorInset = UIEdgeInsetsMake(0, cell.bounds.size.width, 0, 0)

        // Here we add a line at the bottom of the cell (e.g. here at the second row of the second section).
        let additionalSeparatorThickness = CGFloat(1)
        let additionalSeparator = UIView(frame: CGRectMake(0,
            cell.frame.size.height - additionalSeparatorThickness,
            cell.frame.size.width,
            additionalSeparatorThickness))
        additionalSeparator.backgroundColor = UIColor.redColor()
        cell.addSubview(additionalSeparator)
    }
}

如果你不想自己画分隔符,使用这个:

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

这个API只能从ios7开始使用。

使用这个子类,set separatorInset在iOS 9.2.1中不起作用,内容会被压缩。

@interface NSPZeroMarginCell : UITableViewCell

@property (nonatomic, assign) BOOL separatorHidden;

@end

@implementation NSPZeroMarginCell

- (void) layoutSubviews {
    [super layoutSubviews];

    for (UIView *view in  self.subviews) {
        if (![view isKindOfClass:[UIControl class]]) {
            if (CGRectGetHeight(view.frame) < 3) {
                view.hidden = self.separatorHidden;
            }
        }
    }
}

@end

https://gist.github.com/liruqi/9a5add4669e8d9cd3ee9

我的要求是将分隔符隐藏在第4和第5单元格之间。我通过

    -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.row == 3)
    {
        cell.separatorInset = UIEdgeInsetsMake(0, cell.bounds.size.width, 0, 0);
    }
}