viewForHeaderInSection宽度错误 [英] viewForHeaderInSection incorrect width
问题描述
headerView的宽度不适合屏幕的宽度.
我的viewForHeaderInSection
函数:
Width of my headerView does not adapt to the width of the screen.
My viewForHeaderInSection
function:
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell = self.tableView.dequeueReusableCellWithIdentifier("sectionCell") as! sectionCell
cell.hint.text = "some name"
cell.tag = section
let singleTap = UITapGestureRecognizer(target: self, action: #selector(singleTapOnSection(_:)))
cell.addGestureRecognizer(singleTap)
let view = UIView(frame: cell.frame)
view.addConstraints(cell.constraints)
view.addSubview(cell)
return view
}
我必须将单元格添加到UIView(或类似的东西),因为我在该节中隐藏了行. 我认为我必须以编程方式在查看"中添加一些约束,但是我不知道如何
I must add cell to the UIView (or something similar) because i have hiding of row in the section. In my opinion i must add to "view" some constraints programmatically, but i dont know how
推荐答案
定义customView: UITableViewHeaderFooterView
类,并在View Controller的viewDidLoad:
中编写
Define a customView: UITableViewHeaderFooterView
class, and in viewDidLoad:
of View Controller write
tableView.registerClass(customView.classForCoder(), forHeaderFooterViewReuseIdentifier: "HeaderIdentifier")
而不是使单元出队,而是使UITableViewHeaderFooterView
出队(因为这是正确的方法).
And instead of dequeueing a cell, dequeue a UITableViewHeaderFooterView
(as it is the right way to do it).
这样,您必须在代码中添加自动布局或在 nib 文件中定义自动布局,如果您使用的是这种方法,那么与nib一起工作并处理各种事情会让人感到沮丧.使用此库 NibDesignable .
This way you have to add auto layout in code or define it in a nib file, working with nibs and handling all kinds of things is kinda frustrating, if you're using this approach i suggest use this library NibDesignable.
这篇关于viewForHeaderInSection宽度错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!