使用 Swift 将按钮添加到静态 TableView 标题 [英] Adding Buttons to Static TableView Headers with Swift

查看:25
本文介绍了使用 Swift 将按钮添加到静态 TableView 标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 swift 将 UIControls 添加到 iOS 中的静态 TableViewHeaders.目标是建立在默认外观之上,因此 UI 将匹配未来的默认 UI 元素.

I am trying to add UIControls to static TableViewHeaders in iOS using swift. The goal is build on top of the default appearance so the UI will match default UI elements going forward.

我发现了一个修改现有元素外观的很好例子,但没有添加新的.

I found a great example of modifying the appearance of existing elements, but nothing on adding new ones.

我的具体目标是:

  1. 全选"功能可快速将部分中的所有行标记为已选中"(可能是选中标记附件、UISwitch 等)
  2. 显示/隐藏"功能允许单个视图按部分提供简单的概览",同时仍提供对部分内详细信息的访问.

由于这两个都与部分分组相关,因此标题是添加此功能的合乎逻辑的选择.

As both of these related to the section groupings, the headers are a logical choice for adding this functionality.

推荐答案

为 TableView 自定义静态标题有两个主要选项:

There are two main options for customizing static headers for TableViews:

willDisplayHeaderView 提供默认视图并允许对其进行修改.简单的外观修改相当简单.添加按钮等交互功能有点复杂

willDisplayHeaderView provides the default view and allows for modification of it. Simple appearance modifications are fairly straight forward. Adding interactive features such as buttons is a bit more complicated

viewForHeaderInSection 返回标题的视图,它可以从笔尖加载或完全在代码中创建.这样做的一个缺点是它无法访问标头的默认外观,而 Apple 仅提供对一种默认外观的访问权限 (UIColor.groupTableViewBackground).

viewForHeaderInSection returns the view for the header, which can be loaded from a nib or created entirely in code. One disadvantage of this is that it gives no access to the default appearance of headers, and Apple only provides access to one default (UIColor.groupTableViewBackground).

要在默认标题之上构建,需要使用 willDisplayHeaderView.

To build on top of the default header, willDisplayHeaderView needs to be used.

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int)
{    
    let header = view as! UITableViewHeaderFooterView
    let headerLabelFont: UIFont = header.textLabel!.font

    // References for existing or new button
    var selectButton: UIButton? = nil

    // Check for existing button
    for i in 0..<view.subviews.count {
      if view.subviews[i] is UIButton {
          selectButton = view.subviews[i] as? UIButton
      }
    }

    // No button exist, create new one
    if selectButton == nil {
      selectButton = UIButton(type: .system)
      header.addSubview(selectButton!)
      toggleButton = UIButton(type: .system)
      header.addSubview(toggleButton!)
    }
    // Configure button
    selectButton?.frame = CGRect(x: view.frame.size.width - 85, y: view.frame.size.height - 28, width: 77, height: 26)
    selectButton?.tag = section
    selectButton?.setTitle("SELECT ALL", for: .normal)
    selectButton?.titleLabel?.font = UIFont(descriptor: headerLabelFont.fontDescriptor, size: 11)
    selectButton?.contentHorizontalAlignment = .right;
    selectButton?.setTitleColor(self.view.tintColor, for: .normal)
    selectButton?.addTarget(self, action: #selector(self.selectAllInSection), for: .touchUpInside)

}

func selectAllInSection() {
    ...

最大的挑战是解决静态标题单元格可以被 TableView 重用的事实.因此,如果修改了一个按钮,例如通过添加一个按钮,那么当它被重用时,可以添加第二个按钮.这仅在 TableView 大到足以滚动到屏幕外时才会出现问题,但应加以防范,因为结果可能难以追踪.

The biggest challenge is working around the fact that static header cells can be reused by the TableView. So if one is modified, for example by adding a button, then when it is reused a second button could be added. This is only a problem if the TableView is large enough to scrolll off screen, but should be guarded against as the results can be tough to track down.

如果将多个按钮添加到标题中,则需要某种机制来识别每个按钮.UIButton.tag 是一种选择,但在本示例中,该字段用于标识要对其执行操作的部分.另一种选择是使用标签字符串来包含两条信息.

If multiple buttons are added to a header, some mechanism to identify each button is needed. The UIButton.tag is one option, but in this example that field is used to identify which section to act on. Another option would be to use the tag string to include two pieces of information.

可以在 Github

(是的,回答我自己的问题,在浸出多年后想回馈一些东西)

(yes answering my own question, wanted to give something back after leaching for years)

这篇关于使用 Swift 将按钮添加到静态 TableView 标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆