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

标签 ios uitableview swift

在我的 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
}

最佳答案

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

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 添加为另一个 subview 。

关于ios - 我们如何使用 swift 在 UITableView 部分标题中添加图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28600375/

相关文章:

ios - Swift 无法将类型 '()' 的值分配给类型 'String?'

ios - 如何获取在 UITableView 中选择的单元格的类(自定义)?

ios - 图像正在模拟器的 TableView 中加载,但不在设备中

ios - Xcode "Id: framework not found Pods"中的 Pod 错误

ios - iOS FirebaseCrashlytics upload_symbols和运行脚本不在FirebaseCrashlytics.framework中

ios - 动态 tableViewCell 高度

iphone - 如何动态设置 ScrollView 的内容大小

iphone - 当我有多个部分时,如何以数字 0 开始我的 UITableView 部分?

swift - 如何用动画改变单元格编辑风格

ios - 数组元素连续追加