【问题标题】:Swift - how to make custom header for UITableView?Swift - 如何为 UITableView 制作自定义标题?
【发布时间】:2015-11-05 01:06:47
【问题描述】:

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

我试试这个

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
}

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

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

【问题讨论】:

  • 不需要将view 添加到self.view。它将从那里移除并添加到表格视图中。
  • @Cyrille 我不确定为什么您认为视图在以编程方式添加到 subviews 数组后会被删除,但在我看来,它只是以两种不同的方式添加到视图层次结构中地点,这将导致未定义的行为。
  • 不。一个视图一次只能有一个超级视图。如果您在任何地方添加视图,它将首先从之前的位置删除。

标签: ios objective-c swift uitableview


【解决方案1】:

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

view.addSubview(label)

【讨论】:

  • 你需要添加标签,是的;但不需要将view 添加到self.view。它将从那里移除并添加到表格视图中。
【解决方案2】:

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

self.tableView.sectionHeaderHeight = 70

另外你应该替换

self.view.addSubview(view)

通过

view.addSubview(label)

最后你必须检查你的框架

let view = UIView(frame: CGRect.zeroRect)

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

【讨论】:

  • 非常感谢!特别是关于 ViewDidLoad 的评论。现在效果很好
【解决方案3】:

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

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

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

如果你想有简单的视图,添加以下内容。

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

【讨论】:

    【解决方案4】:

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

    为 swift 3.0 更新

    第 1 步

    为自定义标题创建 UITableViewHeaderFooterView..

    import UIKit
    
    class MapTableHeaderView: UITableViewHeaderFooterView {
    
        @IBOutlet weak var testView: UIView!
    
    }
    

    第 2 步

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

    【讨论】:

    • 非常好的兄弟。我也学会了使用扩展来管理代码。感谢您提供完整的代码格式! :)
    【解决方案5】:

    这对我有用 - Swift 3

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    
            let headerCell = tableView.dequeueReusableCell(withIdentifier: "customTableCell") as! CustomTableCell
            return headerCell
        }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 40
    }
    

    【讨论】:

    • 太棒了。我手动添加了一个标题单元格,并且必须在适当的委托方法中偏移单元格。这将节省一些工作
    • 当我添加 heightForHeaderInSectionviewForHeaderInSection 时,它对我有用。我在情节提要的表格视图中创建了另一个表格单元格并给出了不同的标识符。这有效!
    【解决方案6】:

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

    #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 = .systemFont(ofSize: 16)
            label.textColor = .yellow
            
            headerView.addSubview(label)
            
            return headerView
        }
    

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

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

    一切准备就绪 ? ? ?

    【讨论】:

    • 如果我们能改进这个,请告诉我??
    • 你不必添加额外的视图,你可以直接创建 UILabel 作为标题。 让 headerView = UILabel.init(frame: CGRect.init(x: 0, y: 0, width: tableView.frame.width, height: 50)), headerView.text = "通知时间"
    • 感谢您回信@Hope,是的,我们可以按照您的建议进行操作,我之所以这样做,是因为我需要按照 Zeplin 遵循我的 UI(我写“我的自定义颜色和字体”的原因) :)
    • 查看隐藏滚动问题
    • 嗨@KrunalNagvadia,您遇到什么问题?
    【解决方案7】:

    我在 Swift 5 中遇到了一些问题。使用此功能时,我与标题单元格对齐错误:

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
         let headerCell = tableView.dequeueReusableCell(withIdentifier: "customTableCell") as! CustomTableCell
         return headerCell
    }
    

    单元格视图显示的对齐错误,并且显示了表格视图的顶部。所以我不得不做一些这样的调整:

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
         let headerView = UIView.init(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 90))
         let headerCell = tableView.dequeueReusableCell(withIdentifier: "YOUR_CELL_IDENTIFIER")
         headerCell?.frame = headerView.bounds
         headerView.addSubview(headerCell!)
         return headerView
     }
    

    我在 Swift 5 和 Xcode 12.0.1 中遇到了这个问题,我不知道这对我来说是一个问题还是一个错误。希望对你有帮助?!我已经失去了一个上午...

    【讨论】:

    • 终于有人解决了我的问题。非常感谢你!
    • 我也失去了一些时间,所以我很乐意提供帮助。我不知道为什么在最后一个 Xcode 版本中这个函数已经结束工作,因为在其他项目中它工作正常。
    猜你喜欢
    • 1970-01-01
    • 2016-05-02
    • 2013-03-14
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 2020-01-19
    • 1970-01-01
    • 2012-08-18
    相关资源
    最近更新 更多