【问题标题】:set an image for table Header View为表格标题视图设置图像
【发布时间】:2017-09-18 21:06:18
【问题描述】:

我正在尝试将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

这段代码给了我

致命错误:在展开可选值时意外发现 nil

【问题讨论】:

  • 您在哪一行收到此错误?
  • 再次检查我的问题

标签: ios swift uitableview swift3


【解决方案1】:

您实际上并没有创建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

【讨论】:

  • ImageView size is zero. Set its frame, call sizeToFit or force layout first. You can also set a custom format with a defined size if you don't want to force layout.
  • 所以添加对sizeToFit() 的呼叫,如我更新的答案所示。
【解决方案2】:

ImageHeader 为零。你从来没有实例化它。同样只有类名应该以大写字母开头。

【讨论】:

  • 我该如何解决这个问题?
猜你喜欢
  • 2015-07-03
  • 1970-01-01
  • 2013-08-08
  • 1970-01-01
  • 2013-05-22
  • 2018-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多