简体   繁体   English

为什么第 header 部分总是显示为灰色且在一行中? 在 Swift

[英]Why section header always showing in grey colour and in one line? in Swift

I am using two tableviews side by side, so need section header text in two lines and in black colour.我并排使用两个表格视图,因此需要两行黑色的 header 部分文本。 but its not coming like that.. why?但它不会那样......为什么?

code: I have used these methods but still not showing header text in black colour and in two lines... please let me know, how to show header text in black and in two lines代码:我已经使用了这些方法,但仍然没有以黑色和两行显示 header 文本...请告诉我,如何以黑色和两行显示 header 文本

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

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    (view as! UITableViewHeaderFooterView).textLabel?.textColor = UIColor.black
    (view as! UITableViewHeaderFooterView).textLabel?.numberOfLines = 2

}

func numberOfSections(in tableView: UITableView) -> Int {
    if tableView == tableView1 {
        return nonProSubategory.count
    } else if tableView == tableView2 {
        return proSubategory.count
    }
    return 0
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    if tableView == tableView1 {
        return nonProSubategory[section].title
    } else if tableView == tableView2 {
        return proSubategory[section].title
    }
    return ""
}

o/p: with this code text always showing in grey colour and in one line.. here "beauty,health.." and "home services" are header text o/p:此代码文本始终以灰色显示在一行中.. 这里“美容、健康..”和“家庭服务”是 header 文本

Screen of header colour in grey and one line header 彩色灰色单行屏幕

EDIT:编辑:

if i use only these two methods:如果我只使用这两种方法:

 func numberOfSections(in tableView: UITableView) -> Int {
    if tableView == tableView1 {
        return nonProSubategory.count
    } else if tableView == tableView2 {
        return proSubategory.count
    }
    return 0
}
private func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    let v = UILabel()
    v.textColor = .black
    v.numberOfLines = 2

    // you can set a font here, if desired
    //  for example:
    //v.font = .italicSystemFont(ofSize: 15)

    if tableView == tableView1 {
        v.text = nonProSubategory[section].title
    } else if tableView == tableView2 {
        v.text = proSubategory[section].title
    }
    return v
}

o/p: here header title("beauty,health.." and "home services") is missing.. o/p:这里缺少 header title("beauty,health.." and "home services")..

edit code o/p编辑代码o/p

You could take your approach... however...可以采取你的方法......但是......

First, you should always properly cast classes and unwrap optionals:首先,您应该始终正确地转换类并解开可选项:

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    if let v = view as? UITableViewHeaderFooterView,
       let label = v.textLabel
    {
        label.textColor = .black
        label.numberOfLines = 2
    }
}

The default .textLabel has been deprecated though, plus I don't think .numberOfLines is going to work.不过,默认的.textLabel已被弃用,而且我认为.numberOfLines不会起作用。

Much better approach is to return your own view for the section header.更好的方法是为 header 部分返回您自己的视图。

This example uses a UILabel as the view to return, but you can return a more complex view with various subviews, if you wanted to:此示例使用UILabel作为要返回的视图,但如果您愿意,可以返回带有各种子视图的更复杂的视图:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    
    let v = UILabel()
    v.textColor = .black
    v.numberOfLines = 2
    
    // you can set a font here, if desired
    //  for example:
    //v.font = .italicSystemFont(ofSize: 15)
    
    if tableView == tableView1 {
        v.text = nonProSubategory[section].title
    } else if tableView == tableView2 {
        v.text = proSubategory[section].title
    }
    return v
}

Then get rid of titleForHeaderInSection , willDisplayHeaderView and heightForHeaderInSection (height will be handle be auto-layout).然后摆脱titleForHeaderInSectionwillDisplayHeaderViewheightForHeaderInSection (高度将被处理为自动布局)。

There are multiple ways to add section headers to a table view like having it inside UITableView if its inside UIViewController in a Storyboards or in an external custom view like nibs.有多种方法可以将节标题添加到表视图中,例如将其放在 UITableView 中,如果它在故事板中的 UIViewController 中或在像笔尖这样的外部自定义视图中。 Include how did you add your section header in the code.包括你是如何在代码中添加你的部分 header 的。 Try to use story boards and automatic dimensions for this.尝试为此使用故事板和自动尺寸。 Yes, you can restrict no of lines if needed.是的,如果需要,您可以限制行数。

Some samples一些样品

https://santoshkumarjm.medium.com/how-to-create-custom-header-and-footer-view-for-tableview-using-xib-in-swift-23959d81d5c4 https://santoshkumarjm.medium.com/how-to-create-custom-header-and-footer-view-for-tableview-using-xib-in-swift-23959d81d5c4

https://developer.apple.com/documentation/uikit/views_and_controls/table_views/adding_headers_and_footers_to_table_sections https://developer.apple.com/documentation/uikit/views_and_controls/table_views/adding_headers_and_footers_to_table_sections

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM