ios Swift - 如何为 UITableView 制作自定义标题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31964941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 07:13:55  来源:igfitidea点击:

Swift - how to make custom header for UITableView?

iosobjective-cswiftuitableview

提问by Alexey K

I need to add custom header to my table

我需要在我的表格中添加自定义标题

I try this

我试试这个

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    let view = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 18))
    let label = UILabel(frame: CGRect(x: 20, y: 20, width: 50, height: 50))
    label.text = "TEST TEXT"
    label.textColor = UIColor.whiteColor()

    self.view.addSubview(view)

    return view
}

but this doesn't work, I see nothing on table

但这不起作用,我在桌子上看不到任何东西

What am I doing wrong ? Or maybe there is another ways ?

我究竟做错了什么 ?或者也许还有其他方法?

采纳答案by Tanguy G.

Did you set the section header height in the viewDidLoad?

您是否在 viewDidLoad 中设置了节标题高度?

self.tableView.sectionHeaderHeight = 70

Plus you should replace

另外你应该更换

self.view.addSubview(view)

by

经过

view.addSubview(label)

Finally you have to check your frames

最后你必须检查你的框架

let view = UIView(frame: CGRect.zeroRect)

and eventually the desired text color as it seems to be currently white on white.

并最终获得所需的文本颜色,因为它目前似乎是白底白字。

回答by Mohit G.

The best working Solution of adding Custom header view in UITableView for section in swift 4 is --

在 UITableView 中为 swift 4 中的部分添加自定义标题视图的最佳工作解决方案是——

1 first Use method ViewForHeaderInSection as below -

1 首先使用方法 ViewForHeaderInSection 如下 -

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: tableView.frame.width, height: 50))

        let label = UILabel()
        label.frame = CGRect.init(x: 5, y: 5, width: headerView.frame.width-10, height: headerView.frame.height-10)
        label.text = "Notification Times"
        label.font = UIFont().futuraPTMediumFont(16) // my custom font
        label.textColor = UIColor.charcolBlackColour() // my custom colour

        headerView.addSubview(label)

        return headerView
    }

2 Also Don't forget to set Height of the header using heightForHeaderInSection UITableView method -

2 另外不要忘记使用 heightForHeaderInSection UITableView 方法设置标题的高度 -

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 50
    }

and you're all set Check it here in image

你已经准备好了 在图片中检查它

回答by GayashanK

If you are willing to use custom table header as table header, try the followings....

如果您愿意使用自定义表头作为表头,请尝试以下....

Updated for swift 3.0

为 swift 3.0 更新

Step 1

第1步

Create UITableViewHeaderFooterView for custom header..

为自定义标题创建 UITableViewHeaderFooterView ..

import UIKit

class MapTableHeaderView: UITableViewHeaderFooterView {

    @IBOutlet weak var testView: UIView!

}

Step 2

第2步

Add custom header to UITableView

向 UITableView 添加自定义标题

    override func viewDidLoad() {
            super.viewDidLoad()

            tableView.delegate = self
            tableView.dataSource = self

            //register the header view

            let nibName = UINib(nibName: "CustomHeaderView", bundle: nil)
            self.tableView.register(nibName, forHeaderFooterViewReuseIdentifier: "CustomHeaderView")


    }

    extension BranchViewController : UITableViewDelegate{

    }

    extension BranchViewController : UITableViewDataSource{

        func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
            return 200
        }

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

            return headerView
        }

        func tableView(_ tableView: UITableView, numberOfRowsInSection section: 

    Int) -> Int {
            // retuen no of rows in sections
        }

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
            // retuen your custom cells    
        }

        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        }

        func numberOfSections(in tableView: UITableView) -> Int {
            // retuen no of sections
        }

        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            // retuen height of row
        }


    }

回答by A.G

If you are using custom cell as header, add the following.

如果您使用自定义单元格作为标题,请添加以下内容。

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

        let headerView = UIView()
        let headerCell = tableView.dequeueReusableCell(withIdentifier: "customTableCell") as! CustomTableCell
        headerView.addSubview(headerCell)
        return headerView
    }

If you want to have simple view, add the following.

如果您想要简单的视图,请添加以下内容。

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView:UIView =  UIView()
    return headerView
}

回答by Basir Alam

This worked for me - Swift 3

这对我有用 - Swift 3

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

        let headerCell = tableView.dequeueReusableCell(withIdentifier: "customTableCell") as! CustomTableCell
        return headerCell
    }

回答by Anbu.Karthik

add labelto subviewof custom view, no need of self.view.addSubview(view), because viewForHeaderInSectionreturn the UIView

添加labelsubview自定义view,不需要self.view.addSubview(view),因为viewForHeaderInSection返回UIView

view.addSubview(label)