0

我的 headerView 的宽度不适应屏幕的宽度。我的viewForHeaderInSection功能:

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 (或类似的东西),因为我在该部分中隐藏了行。在我看来,我必须以编程方式添加“查看”一些约束,但我不知道如何

4

2 回答 2

0

您应该headerview使用相同的宽度tableview和高度来定义您需要的东西。不要给出单元格的高度或宽度。如果您使用的是 ui 的 storyborard,然后将 uiview 拖到上侧的 tableview 上,它将自动设置为 headerview,然后给出top,leading,trailing and fix heigth constraints和删除viewForHeaderInSection方法。

希望这会有所帮助:)

于 2016-04-18T18:08:32.323 回答
0

定义一个customView: UITableViewHeaderFooterView类,并在viewDidLoad:View Controller 中写

tableView.registerClass(customView.classForCoder(), forHeaderFooterViewReuseIdentifier: "HeaderIdentifier")

而不是出列一个单元格,出列一个UITableViewHeaderFooterView(因为这是正确的方法)。

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

于 2016-04-18T18:16:59.793 回答