ios – Swift – 如何创建自定义viewForHeaderInSection,使用XIB文件?

ios – Swift – 如何创建自定义viewForHeaderInSection,使用XIB文件?,第1张

概述我可以通过如下编程方式创建简单的自定义viewForHeaderInSection.但是我想要做更复杂的事情,也许与不同的类连接,并且像tableView单元格一样实现它们的属性.简单来说,我想看看我做了什么. func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 我可以通过如下编程方式创建简单的自定义viewForheaderInSection.但是我想要做更复杂的事情,也许与不同的类连接,并且像tableVIEw单元格一样实现它们的属性.简单来说,我想看看我做了什么.
func tableVIEw(tableVIEw: UItableVIEw,vIEwForheaderInSection section: Int) -> UIVIEw? {    if(section == 0) {        let vIEw = UIVIEw() // The wIDth will be the same as the cell,and the height should be set in tableVIEw:heightForRowAtIndexPath:        let label = UILabel()        let button   = UIbutton(type: UIbuttonType.System)        label.text="My Details"        button.setTitle("Test Title",forState: .normal)        // button.addTarget(self,action: Selector("visibleRow:"),forControlEvents:.touchUpInsIDe)        vIEw.addSubvIEw(label)        vIEw.addSubvIEw(button)        label.translatesautoresizingMaskIntoConstraints = false        button.translatesautoresizingMaskIntoConstraints = false        let vIEws = ["label": label,"button": button,"vIEw": vIEw]        let horizontallayoutContraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|-10-[label]-60-[button]-10-|",options: .AlignAllCenterY,metrics: nil,vIEws: vIEws)        vIEw.addConstraints(horizontallayoutContraints)        let verticalLayoutContraint = NSLayoutConstraint(item: label,attribute: .CenterY,relatedBy: .Equal,toItem: vIEw,multiplIEr: 1,constant: 0)        vIEw.addConstraint(verticalLayoutContraint)        return vIEw    }    return nil}func tableVIEw(tableVIEw: UItableVIEw,heightForheaderInSection section: Int) -> CGfloat {    return 50}

有没有人可以解释如何使用xib创建自定义tableVIEw标题视图?我遇到了旧的Obj-C主题,但我是Swift语言的新功能.如果有人详细解释,会很棒.

1.issue: button @IBAction doesn’t connect with my VIEwController. (Fixed)

解决与文件的所有者,VIEwController基类(点击左侧轮廓菜单.)

2.issue: header height problem (Fixed)

解决在vIEwForheaderInSection:方法中添加headerVIEw.clipsToBounds = true.

对于约束警告this answer solved my problems:

当在VIEwController中使用此方法添加ImageVIEw时,甚至使用相同的高度约束,它将流过tableVIEw行look like picture.

func tableVIEw(tableVIEw: UItableVIEw,heightForheaderInSection section: Int) -> CGfloat {    return 120}

如果我使用,在vIEwDIDLoad中自动调整ScrollVIEwInsets,在这种情况下,图像在导航栏下流动. -固定-

self.automaticallyAdjustsScrollVIEwInsets = false

3.issue: If button under VIEw (Fixed)

@IBAction func dIDTapbutton(sender: AnyObject) {    print("tapped")    if let upVIEw = sender.supervIEw {        if let headerVIEw = upVIEw?.supervIEw as? Customheader {            print("in section \(headerVIEw.sectionNumber)")        }    }}
解决方法 基于NIB的标头的典型过程是:

>创建UItableVIEwheaderfooterVIEw子类,至少有一个标签的插座.您可能还想给它一些标识符,您可以通过该标识符反向工程到该标题对应的部分.同样,您可能需要指定一个协议,通过该协议,头可以通知视图控制器的事件(例如敲击按钮).因此,在Swift 3中:

// if you want your header to be able to inform vIEw controller of key events,create protocolprotocol CustomheaderDelegate: class {    func dIDTapbutton(in section: Int)}// define Customheader class with necessary `delegate`,`@IBOutlet` and `@IBAction`:class Customheader: UItableVIEwheaderfooterVIEw {    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    @IBAction func dIDTapbutton(_ sender: AnyObject) {        delegate?.dIDTapbutton(in: sectionNumber)    }}

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

>创建视图NIB,或者如果您启动了一个空的NIB,则将视图添加到NIB;
>将视图的基类设置为您的UItableVIEwheaderfooterVIEw子类(在我的示例中为Customheader);
>在IB中添加您的控制和限制;
钩住@IBOutlet引用Swift代码的插座;
>将按钮连接到@IBAction

>在视图控制器中的vIEwDIDLoad中注册NIB.在Swift 3:

overrIDe func vIEwDIDLoad() {    super.vIEwDIDLoad()    tableVIEw.register(UINib(nibname: "Customheader",bundle: nil),forheaderfooterVIEwReuseIDentifIEr: "Customheader")}

或在Swift 2:

overrIDe func vIEwDIDLoad() {    super.vIEwDIDLoad()    tableVIEw.registerNib(UINib(nibname: "Customheader",forheaderfooterVIEwReuseIDentifIEr: "Customheader")}

>在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}

或者,在Swift 2:

overrIDe func tableVIEw(tableVIEw: UItableVIEw,vIEwForheaderInSection section: Int) -> UIVIEw? {    let headerVIEw = tableVIEw.dequeueReusableheaderfooterVIEwWithIDentifIEr("Customheader") as! Customheader    headerVIEw.customLabel.text = content[section].name    headerVIEw.sectionNumber = section    headerVIEw.delegate = self    return headerVIEw}overrIDe func tableVIEw(tableVIEw: UItableVIEw,heightForheaderInSection section: Int) -> CGfloat {    return 44  // or whatever}

>显然,如果要将视图控制器指定为标题视图中按钮的代理,则必须符合该协议:

extension VIEwController: CustomheaderDelegate {    func dIDTapbutton(in section: Int) {        print("\(section)")    }}

当我列出所有涉及的步骤时,这听起来很混乱,但一旦你完成了一两次,这真的很简单.我认为这比通过编程方式构建标题更简单.

总结

以上是内存溢出为你收集整理的ios – Swift – 如何创建自定义viewForHeaderInSection,使用XIB文件?全部内容,希望文章能够帮你解决ios – Swift – 如何创建自定义viewForHeaderInSection,使用XIB文件?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1111114.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存