繁体   English   中英

如何删除UITableView中最后一个单元格的最后一个边框?

[英]How to remove the last border of the last cell in UITableView?

在我的应用程序中,我使用UITableView我的问题是我想删除UITableView中最后一个单元格的最后一个边框。

请检查以下图片:

在此处输入图像描述

最好的解决方案是添加页脚视图。 以下代码完美地隐藏了最后一个单元格的行:

Objective-C

self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 1)];

斯威夫特 4.0

tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 1))

在 iOS 7 中有一个更简单的解决方案。 假设单元格是您的最后一个单元格:

cell.separatorInset = UIEdgeInsetsMake(0, cell.bounds.size.width, 0, 0);

于 2015 年 9 月 14 日更新。 我原来的答案已经过时了,但它仍然是所有 iOS 版本的通用解决方案:

您可以隐藏tableView的标准分隔线,并在每个单元格的顶部添加您的自定义行。 添加自定义分隔符最简单的方法是添加 1px 高度的简单UIView

UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cell.bounds.size.width, 1)];
separatorLineView.backgroundColor = [UIColor grayColor];
[cell.contentView addSubview:separatorLineView];

迄今为止,我订阅了另一种在单元格下方隐藏额外分隔符的方法(适用于 iOS 6.1+):

self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

这适用于 iOS 7 和 8:

cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, CGRectGetWidth(tableView.bounds));

注意:使用tableView.bounds时要小心,因为它有时会根据您调用它的时间报告错误的值。 请参阅在横向模式下报告不正确的边界

viewDidLoad()中添加这行代码。

tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 0.001))

这确保最后一个分隔符将被替换并且替换(不可见)页脚视图不会占用任何额外的高度。 由于页脚视图的宽度将由UITableView管理,因此您可以将其设置为 0。

Swift 4 的基于扩展的解决方案,在 iOS 12 上测试

我注意到设置height = 1的空视图也会删除最后一个可见单元格的分隔符,但是设置height = 0只会删除空单元格的分隔符

extension UITableView {
    func removeSeparatorsOfEmptyCells() {
        tableFooterView = UIView(frame: .zero)
    }

    func removeSeparatorsOfEmptyCellsAndLastCell() {
        tableFooterView = UIView(frame: CGRect(origin: .zero, size: CGSize(width: 0, height: 1)))
    }
}

我的较短版本:

self.tblView.tableFooterView = [UIView new];

在 Swift 中:

tableView.tableFooterView = UIView()

这是我目前使用的补救措施。 这可能不是最好的方法,但它确实有效。

删除 SeparatorColor 和 setSeparatorStyle 以制作自定义分隔符

- (void)viewDidLoad
{
    [super viewDidLoad];

    ...

    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.tableView setSeparatorColor:[UIColor clearColor]];
}

为每个具有表格视图背景的单元格添加自定义分隔符

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    ...

    UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, cell.frame.size.height-1, 320, 1)];
    separatorLineView.backgroundColor = self.tableView.backgroundColor;
    [cell.contentView addSubview:separatorLineView];

    return cell;
}
_tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

适用于 iOS 7 及更高版本

    -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        BOOL bIsLastRow = NO;

        //Add logic to check last row for your data source
        NSDictionary *dict = [_arrDataSource objectAtIndex:indexPath.section];
        if([_arrDataSource lastObject] == dict)
        {
           bIsLastRow = YES;
        }

        //Set last row separate inset left value large, so it will go outside of view
        if ([cell respondsToSelector:@selector(setSeparatorInset:)]) 
        {
            [cell setSeparatorInset:UIEdgeInsetsMake(0, bIsLastRow ? 1000 :0, 0, 0)];
        }
    }

下面的代码帮助我从 UITableView 的最后一行删除分隔符。

斯威夫特 3.0

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if indexPath.row == tableView.numberOfRows(inSection: indexPath.section) {
        cell.separatorInset.right = cell.bounds.size.width
    }
}

斯威夫特 4.2

要在最后一个单元格中获得从左到右的分隔符,请将其添加到UITableViewDataSource's tableView(_:cellForRowAt:)实现中:

if tableView.numberOfRows(inSection: indexPath.section) - 1 == indexPath.row {
        cell.separatorInset = .zero
}

如果您不想为某个特定单元格设置分隔符,请考虑绘制自己的分隔符并设置tableView.separatorStyle = .none

Xcode 12,斯威夫特 5

要在最后一个单元格后删除分隔符,请选择分组 UITableView 样式您可以在 Interface Builder 中执行此操作或在 viewDidLoad 中设置它

tableView.style = UITableView.Style.grouped

删除第一个单元格之前的空白,在 viewDidLoad 中编写以下代码:

tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.width, height: 0.1))

要删除单元格之前的空白空间,并且您将在导航栏下滚动它或在避免空白空间方面遇到其他困难,请尝试将您的表格约束到控制器的超级视图这里双击顶部约束,然后选择“SuperView.Top '在约束设置

如果您想要全宽分隔符或您自己的宽度,只需选择 Separator Inset 作为自定义,并配置为您的值,或将其写入 viewDidLoad:

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

cellForRow委托中找到最后一个单元格索引,然后将代码行放在下面:

cell.separatorInset = UIEdgeInsetsMake(
    0.f,
    40.0f,
    0.f,
    self.view.frame.size.width-40.0f
);

另一种选择是UITableView

@synthesize hideLastSeparator = _hideLastSeparator;
@synthesize hideLastSeparatorView = _hideLastSeparatorView;
-(void)setHideLastSeparator:(BOOL)hideLastSeparator {
    if (_hideLastSeparator == hideLastSeparator) {
        return;
    }
    _hideLastSeparator = hideLastSeparator;
    if (_hideLastSeparator) {
        _hideLastSeparatorView = [[UIView alloc] initWithFrame:CGRectMake(0, self.contentSize.height, self.bounds.size.width, 0.5f)];
        _hideLastSeparatorView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
        _hideLastSeparatorView.backgroundColor = self.backgroundColor;
        [self addSubview:_hideLastSeparatorView];
        [self hideSeparator];
    }
    else {
        [_hideLastSeparatorView removeFromSuperview];
        _hideLastSeparatorView = nil;
    }
}
-(void)setContentSize:(CGSize)contentSize {
    [super setContentSize:contentSize];
    if (_hideLastSeparator) {
        [self hideSeparator];
    }
}
-(void)hideSeparator {
    CGRect frame = _hideLastSeparatorView.frame;
    frame.origin.y = self.contentSize.height - frame.size.height;
    _hideLastSeparatorView.frame = frame;
}

.h 应该只包含hideLastSeparatorhideLastSeparatorView的属性声明。
当想要隐藏分隔符时,使用新类并设置myTableView.hideLastSeparator = YES
它的工作方式是通过在最后一个分隔符上添加一个新视图来阻止它。

在我看来,这更容易使用(比使用自定义分隔符,或设置最后一个单元格的最后一个 separatorInset),并且避免了设置tableFooterView的方法有时会导致的一些奇怪的动画(例如在行插入/删除或其他表格动画)。

如果您为UITableViewCell使用自定义分隔符,最好的解决方案是:

  1. 在您的自定义单元格函数中实现,隐藏separatorView
class YourCell: UITableViewCell {
    // Your implementation goes here...
    // Separator view in cell
    @IBOutlet private weak var separatorView: UIView!

    // This function hides the separator view
    func hideSeparator() {
        separatorView.isHidden = true
    }
}
  1. tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)计算单元格是否是最后一个并隐藏它的分隔符
public func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    guard let cell = cell as? YourCell else {
            return
    }
    // Here we're checking if your cell is the last one
    if indexPath.row == tableView.numberOfRows(inSection: indexPath.section) - 1 {
        // if true -> then hide it
        cell.hideSeparator()
    }
}

这对我很有用:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    guard let cell = cell as? yourCell else {
            return
    }
    // Here we're checking if your cell is the last one
    if indexPath.row == numberOfCells.count - 1 {
        cell.separatorInset.left = UIScreen.main.bounds.width
    }
}

我们在这里所做的是将左侧插图的大小设置为足够大的值,以便分隔线不再可见。 因此,当我们增加插图的大小值时,它会越来越靠近屏幕右侧,因为左侧插图朝向正确的方向。

我测试并在 Swift 5、iOS 15 和 xCode 13.3.1 中工作,只需在返回单元格之前将此代码粘贴到 TableView“cellForRowAt”方法中,因为它将最后一行推出框架......注意:-(“ arrayCartItems”将是您在“numberOfRowsInSection”中返回的数组,“tblVw”是您在 ViewController 中的 UITableView 的 Outlet 名称)详细编写,以便即使是新手也可以轻松学习:)

    if indexPath.row == arrayCartItems.count - 1 {
        cell.separatorInset = UIEdgeInsets.init(top: 0, left: tblVw.bounds.width + 1, bottom: 0, right: 0)
    } else {
        cell.separatorInset = .zero
    }

最合适和最简单的方法是在cellForRowAt使用它

cell.drawBottonLine = (indexPath.row != sections[0].rows.count - 1)

扩展 Tonny Xu 的回答,我有多个部分,这似乎适用于删除最后一个单元格分隔符

if(indexPath.row == [self.tableView numberOfRowsInSection:indexPath.section] - 1)
{
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 1)];
}

cellForRowAtIndexPath数据源中

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM