我们如何使用swift在UITableView节标题中添加图像? [英] How can we add image in UITableView section header using swift?
本文介绍了我们如何使用swift在UITableView节标题中添加图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的willDisplayHeaderView中,我更改了节标题的颜色,但是我想在节标题之前添加一个图像.有什么帮助吗? 我的willDisplayHeaderView代码是
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
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
}
推荐答案
您可以设计自己的标头,就像在表格视图中设计自定义单元格一样. 或者,您可以像这样添加图像:
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
}
然后,您可以简单地将部分标题UILabel添加为另一个子视图.
Then you can simply add your section title UILabel as another subview.
这篇关于我们如何使用swift在UITableView节标题中添加图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文