ios - 为表格标题 View 设置图像

标签 ios swift uitableview swift3

我正在尝试将 tableView.tableHeaderView 设置为图像

 var ImageHeader: UIImageView!
    let url = "https://i.pinimg.com/564x/f3/d6/71/f3d671c71d6f19d34f856103758ff203.jpg"


    ImageHeader.hnk_setImage(from: URL(string: url)) // error here
    tableView.estimatedSectionHeaderHeight = 40.0
    self.automaticallyAdjustsScrollViewInsets = false
    let header = UIView(frame : CGRect(x : 0,y:0, width : tableView.frame.width , height : 100))
    header.addSubview(ImageHeader)
    tableView.tableHeaderView = header

这段代码给了我

fatal error: unexpectedly found nil while unwrapping an Optional value

最佳答案

您实际上并没有创建 UIImageView。所以 ImageHeadernil 并且你强制解包它。轰!

尝试这样的事情:

let url = "https://i.pinimg.com/564x/f3/d6/71/f3d671c71d6f19d34f856103758ff203.jpg"

var imageHeader = UIImageView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
imageHeader.hnk_setImage(from: URL(string: url))
let header = UIView(frame : CGRect(x : 0,y:0, width : tableView.frame.width , height : 100))
header.addSubview(imageHeader)
tableView.tableHeaderView = header

关于ios - 为表格标题 View 设置图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46288214/

相关文章:

Swift:在协议(protocol)扩展中初始化对象后调用一些方法

ios - 观察 UICollectionView 的 visibleCells 数组的变化

ios - 从两个单独的 firebase 数据库中加载两个到 tableview swift

ios - 在 iOS 的 TableView 单元格中显示 Collection View 的问题

ios - 无法使用 int 类型的右值初始化 UIRemote NotificationType 类型的参数

ios - PassKit 不可预测的崩溃

ios - 如何在每个 child 的内部以不同的方式转换 super 变量?

ios - 如何在 swift 中通过代码添加导航栏并显示另一个 Controller

iphone - Xcode 4.2 "cell reuse indentifier in nib (Cell) does not match the identifier used to register the nib (ThisCell)"

ios - 在 objective-c 中生成初始化向量