栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

  1. 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)}

    }

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

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

    • 将视图的基类设置为您的

      UITableViewHeaderFooterView
      子类(在我的示例中为
      CustomHeader
      );

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

    • 胡克

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

    • 将按钮连接到

      @IBAction
      ; 和

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

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

    override func viewDidLoad() {super.viewDidLoad()tableView.register(UINib(nibName: "CustomHeader", bundle: nil), forHeaderFooterViewReuseIdentifier: CustomHeader.reuseIdentifier)

    }

  4. 在中

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

    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "CustomHeader") as! CustomHeaderheaderView.customLabel.text = content[section].name  // set this however is appropriate for your app's modelheaderView.sectionNumber = sectionheaderView.delegate = selfreturn headerView

    }

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

  5. 显然,如果要将视图控制器指定

    delegate
    为标题视图中按钮的,则必须遵循该协议:

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

    }

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


在马特的回答,他抗议:

问题很简单,就是您不能仅仅通过在身份检查器中声明将

UIView
笔尖神奇地变成笔尖
UITableViewHeaderFooterView

这根本是不正确的。如果使用上述基于NIB的方法,则为此头视图的根视图实例化的类

UITableViewHeaderFooterView
子类,而不是
UIView
。它实例化为NIB根视图的基类指定的任何类。

但是,正确的是,

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

但是,不能断定一个人不能使用

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




转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/616368.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号