Ios viewForHeaderInSection不正确的宽度

Ios viewForHeaderInSection不正确的宽度,ios,uitableview,swift2.2,Ios,Uitableview,Swift2.2,我的头视图的宽度与屏幕的宽度不匹配。 MyViewForHeader部分功能: func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { let cell = self.tableView.dequeueReusableCellWithIdentifier("sectionCell") as! sectionCell cell.hint.text = "s

我的头视图的宽度与屏幕的宽度不匹配。 My
ViewForHeader部分
功能:

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(或类似内容),因为我在节中隐藏了行。
在我看来,我必须以编程的方式向“视图”添加一些约束,但我不知道如何定义
headerview
,使用与
tableview
相同的宽度和所需的高度。不要给出单元格的高度或宽度。如果您正在使用storyborard for ui,则将uiview拖动到上方的tableview上,它将自动设置为headerview,然后给出
top、leading、Training和fix heigth约束
,并删除
ViewForHeaderInterface
方法


希望这会有所帮助:)

定义一个
customView:UITableViewHeaderFooterView
类,并在视图控制器的
viewdiload:
中写入

tableView.registerClass(customView.classForCoder(), forHeaderFooterViewReuseIdentifier: "HeaderIdentifier")
而不是让一个单元格出列,而是让一个
UITableViewHeaderFooterView
出列(因为这是正确的方法)


这样,您必须在代码中添加自动布局或在nib文件中定义自动布局,使用nib和处理各种事情有点令人沮丧,如果您使用这种方法,我建议使用此库。

我回答了另一种处理方法,但我认为您的问题是;您正在添加
cell
作为视图的子视图,但必须添加
cell.contentView
。也可以使用
cell.contentView.constraints
代替
cell.constraints
,但我需要ViewForHeaderSection,因为我在一个表视图中有许多动态创建的节,其中包含不同的文本,我需要将UITapGestureRecognizer添加到一些标题中,这些标题将以这种方式展开或折叠此部分,如上所述,但这会给我一些警告和错误,我无法使其正常工作“无法在捆绑包中加载NIB”,也无法解释您看到的问题是什么?它非常易于使用,您不必再担心nib加载等问题。对于使用
NibDesignable
和reusableCells以及reusableViews,也可以进行检查。