您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Swift-如何使用XIB文件创建自定义viewForHeaderInSection?

Swift-如何使用XIB文件创建自定义viewForHeaderInSection?

基于NIB的标头的典型过程为:

UITableViewHeaderFooterView至少创建一个带有标签出口的子类。您可能还想给它一些标识符,通过它可以反向工程此头对应的部分。同样,您可能希望指定一个协议,标题可以通过该协议将事件通知给视图控制器(例如单击按钮)。因此,在Swift 3及更高版本中:

// if you want your header to be able to inform view controller of key events, create protocol

protocol CustomHeaderDelegate: class { func customHeader(_ customHeader: CustomHeader, didTapButtonInSection section: Int) }

// define CustomHeader class with necessary delegate, @IBOutlet and @IBAction:

class CustomHeader: UITableViewHeaderFooterView { static let reuseIdentifier = “CustomHeader”

weak var delegate: CustomHeaderDelegate?

@IBOutlet weak var customLabel: UILabel!

var sectionNumber: Int!  // you don't have to do this, but it can be useful to have reference back to the section number so that when you tap on a button, you kNow which section you came from; obvIoUsly this is problematic if you insert/delete sections after the table is loaded; always reload in that case

@IBAction func didTapButton(_ sender: AnyObject) {
    delegate?.customHeader(self, didTapButtonInSection: section)
}

}

创建NIB。就个人而言,我为NIB提供与基类相同的名称,以简化项目中文件的管理并避免混淆。无论如何,关键步骤包括

创建视图NIB,或者如果您以空的NIB开始,则将视图添加到NIB;

将视图的基类设置为您的UITableViewHeaderFooterView子类(在我的示例中为CustomHeader);

在IB中添加您的控件和约束;

胡克@IBOutlet在您的银行代码网点引用;

将按钮连接到@IBAction; 和

对于NIB中的根视图,请确保将背景色设置为“认”,否则您将收到有关更改背景色的烦人警告。

viewDidLoad视图控制器中,注册NIB。在Swift 3及更高版本中:

override func viewDidLoad() {
super.viewDidLoad()

tableView.register(UINib(nibName: "CustomHeader", bundle: nil), forHeaderFooterViewReuseIdentifier: CustomHeader.reuseIdentifier)

}

在中viewForHeaderInSection,使用您在上一步中指定的相同标识符出队可重用视图。完成此操作后,您现在就可以使用插座了,您无需对以编程方式创建的约束进行任何操作,等等。唯一需要考虑的事情(为了使按钮的协议起作用)就是指定其委托。例如,在Swift 3中:

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "CustomHeader") as! CustomHeader

headerView.customLabel.text = content[section].name  // set this however is appropriate for your app's model
headerView.sectionNumber = section
headerView.delegate = self

return headerView

}

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return 44 // or whatever }

显然,如果要将视图控制器指定delegate标题视图中按钮的,则必须遵循该协议:

extension ViewController: CustomHeaderDelegate {
func customHeader(_ customHeader: CustomHeader, didTapButtonInSection section: Int) {
    print("did tap button", section)
}

}

当我列出所有涉及的步骤时,这一切听起来都令人困惑,但是一旦完成一两次,它真的很简单。我认为这比以编程方式构建标题视图更简单。

在马特的回答,他抗议:

问题很简单,就是您不能仅仅通过在身份检查器中声明将UIView笔尖神奇地变成笔尖UITableViewHeaderFooterView

这根本是不正确的。如果使用上述基于NIB的方法,则为此头视图的根视图实例化的类UITableViewHeaderFooterView子类,而不是UIView。它实例化为NIB根视图的基类指定的任何类。

但是,正确的是,contentView在这种基于NIB的方法中未使用此类的某些属性(尤其是)。它确实应该是可选属性,就像textLabeldetailTextLabel一样(或者更好的是,它们应该UITableViewHeaderFooterView在IB中添加适当的支持)。我同意这对Apple来说设计不佳,但它给我留下了草率,特质的细节,但鉴于表视图中的所有问题,这都是一个小问题。例如,非常不寻常的是,这些年来,我们仍然无法在情节提要中完全制作原型页眉/页脚视图,而完全必须依靠这些NIB和类注册技术。

但是,不能断定一个人不能使用register(_:forHeaderFooterViewReuseIdentifier:),这是一种自iOS 6起就一直在使用的API方法,这是不正确的。我们不要将婴儿带洗澡水扔掉。

Swift 2022/1/1 18:18:30 有573人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶