ios 我们如何使用 swift 在 UITableView 部分标题中添加图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28600375/
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 04:53:53  来源:igfitidea点击:

How can we add image in UITableView section header using swift?

iosuitableviewswift

提问by Saqib Omer

In my willDisplayHeaderView I have changed color of section header But I want to add an image before section title. Any help? My code for willDisplayHeaderView is

在我的 willDisplayHeaderView 中,我更改了部分标题的颜色但我想在部分标题之前添加一个图像。有什么帮助吗?我的 willDisplayHeaderView 代码是

func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView 
    header.contentView.backgroundColor = UIColor(red: 238/255, green: 168/255, blue: 15/255, alpha: 0.8)
    header.textLabel.textColor = UIColor.whiteColor()
    header.alpha = 1
}

采纳答案by Anfaje

You could design your own header, like you would design a custom cell in a table view. Or you could just add an image like so:

您可以设计自己的标题,就像在表格视图中设计自定义单元格一样。或者你可以像这样添加一个图像:

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

    var myCustomView: UIImageView
    var myImage: UIImage = UIImage(named: "myImageResource")
    myCustomView.image = myImage

    let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView
    header.addSubview(myCustomView)
    return header
}

Then you can simply add your section title UILabel as another subview.

然后你可以简单地将你的部分标题 UILabel 添加为另一个子视图。

回答by Jabbar

Simply Add the following code in your viewDidLoad()method.

只需在您的viewDidLoad()方法中添加以下代码。

    var frame = CGRectMake(0, 0, self.view.frame.size.width, 200)
    var headerImageView = UIImageView(frame: frame)
    var image: UIImage = UIImage(named: "yourImage")!
    headerImageView.image = image
    youTableView.tableHeaderView = headerImageView

回答by Nik

I use this method below in my proj.

我在下面的项目中使用此方法。

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? 
{
    let uilbl = UILabel()
    //uilbl.numberOfLines = 0
    //uilbl.lineBreakMode = NSLineBreakMode.ByWordWrapping
    //uilbl.text = "blablabla"
    uilbl.sizeToFit()
    uilbl.backgroundColor =  UIColor(patternImage: UIImage(named: "yr-img-name")!)

    return uilbl
}

回答by Saqib Omer

Here it is how we can add custom header in UITableView.

这是我们如何在UITableView.

func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
     let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView
     let imageViewGame = UIImageView(frame: CGRectMake(5, 8, 40, 40));
     let image = UIImage(named: "Games.png");
     imageViewGame.image = image;
     header.contentView.addSubview(imageViewGame)
}

回答by Arvin Sanmuga Rajah

For Swift 3

对于 Swift 3

  override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    let header = view as! UITableViewHeaderFooterView
    header.textLabel?.textColor = UIColor.white
    let headerImage = UIImage(named: "headerBanner.jpg")
    let headerImageView = UIImageView(image: headerImage)
    header.backgroundView = headerImageView
}

回答by kroky

This will work with swift 4

这将适用于 swift 4

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerImage: UIImage = UIImage(named: "image")!
    let headerView = UIImageView(image: headerImage)
    return headerView
}