【问题标题】:How to add a button to the UITableView's footer view but preserve the last cell separator?如何向 UITableView 页脚视图添加按钮但保留最后一个单元格分隔符?
【发布时间】:2017-01-26 16:52:08
【问题描述】:

我想在最后一个表格单元格下方添加一个加号按钮。它应该是这样的:

为了得到这个结果,我使用了这个代码:

let footerView = UIView()
tableView.tableFooterView = footerView

addButton.frame = CGRect(x: 0, y: 0, width: 45, height: 45)
addButton.setTitle("+", for: [])
addButton.titleLabel?.font = UIFont.systemFont(ofSize: 30)

footerView.addSubview(addButton)

addButton.addTarget(self, action: #selector(addSection), for: .touchUpInside)

它看起来正是我想要的,但问题是,由于页脚视图有一个零帧,它里面的加号按钮对触摸事件没有响应。

如果我为页脚视图设置一个框架:

let footerView = UIView(frame: CGRect(x: 0, y: 0, width: 45, height: 45))

按钮开始响应触摸,但最后一个单元格的分隔线不见了:

有没有办法在表格页脚视图中添加一些交互式内容但保留最后的分隔线?

【问题讨论】:

  • 这可能有点困难。您可以尝试添加一个视图,例如 FooterView(sepratorView, yourButton)。
  • 你的意思是自己画分隔符?我知道我能做到。我只是认为有一个更标准的解决方案。
  • 另外,如果我使用自定义分隔符,我将需要自己处理与表格编辑(移动和删除单元格)相关的所有逻辑。
  • 我也有同样的问题。我想知道为什么当我们不给出footerview的大小时,里面的按钮对触摸事件没有响应?

标签: ios swift uitableview


【解决方案1】:

像这样在footerView 的顶部添加一行。根据您的需要设置x 值。

let line = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 1))
line.backgroundColor = UIColor.gray
footerView.addSubview(line)

【讨论】:

  • 我想过这一点,但是当您编辑表格时,此分隔线会发生一些逻辑(动画)。而且我不想自己实现它。
【解决方案2】:

最可靠的方法是不使用默认分隔线,而是手动将其添加到您的单元格中。只需在返回单元格之前将此代码复制到您的单元格中即可:

var separator:UIView!
    if let s = cell.viewWithTag(1000)
    {
        separator = s
    }
    else
    {
        separator = UIView()
        separator.tag = 1000
        separator.backgroundColor = TheColorYouWantForYourSeparator
        separator.translatesAutoresizingMaskIntoConstraints = false
        cell.addSubview(separator)

        // Swiper constraints
        let leadingConstraint = NSLayoutConstraint(item: separator, attribute: .leading, relatedBy: .equal, toItem: cell, attribute: .leading, multiplier: 1, constant: 15)
        let heightConstraint = NSLayoutConstraint(item: separator, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 0.5)
        let bottomConstraint = NSLayoutConstraint(item: cell, attribute: .bottom, relatedBy: .equal, toItem: separator, attribute: .bottom, multiplier: 1, constant:0)
        let trailingConstraint = NSLayoutConstraint(item: cell, attribute: .trailing, relatedBy: .equal, toItem: separator, attribute: .trailing, multiplier: 1, constant: 15)
        cell.addConstraints([bottomConstraint, leadingConstraint, heightConstraint, trailingConstraint])
    }

这会产生一个可配置的分隔符视图。使用约束来决定它的形状。

【讨论】:

  • 是的,我知道我可以自己制作分隔符视图,但我不想这样做。这不是在单元格底部添加线条那么简单。它还涉及处理单元格选择、移动和删除等情况。我相信应该存在更简单的解决方案。
  • 这个解决方案不需要任何额外的处理,因为你总是需要这条线。 (记得禁用表格视图的分隔线)即使单元格是最后一个。还是我误解了你的问题?
  • 不,标准分隔符并不总是可见的。1 - 当您选择一行时,下方和上方的分隔符均被隐藏。 2 - 当您在编辑模式下移动一行时,已移动行的分隔符被隐藏,但它们显示在您要插入行的行的占位符中。也许还有其他情况..
  • 我明白了,那么您有一个更复杂的问题,不幸的是,无论您选择哪种解决方案,您都必须手动管理它。要么您必须对每个单元格进行微观管理,要么在编辑时对 tableviewFooter 进行管理。
  • 我只是想...为什么不让那个按钮简单地成为表格视图中的最后一个单元格呢?唯一的问题是,如果单元格太多,它将消失。另一个想法是,也许您需要重新评估您的设计。您可以将添加按钮放在编辑按钮旁边。如果无法通过代码解决您的问题,请通过设计解决。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-21
  • 2011-01-27
  • 2013-11-15
  • 1970-01-01
  • 2015-12-16
相关资源
最近更新 更多