【问题标题】:Customizing Header and Footer of Table View with Swift使用 Swift 自定义表格视图的页眉和页脚
【发布时间】:2017-03-21 03:35:09
【问题描述】:

我需要为我的表格视图制作一个自定义标题。为此,我创建了一个 UITableViewHeaderFooterView 类型的类,但是我无法在情节提要上选择 tableview 的标题来设置该类。如果它是静态表头,则会出现,但对动态表不可见。如何进行此设置?

注意:我使用的是 xcode 7.3.1

我正在尝试做这样的事情,但情节提要: https://github.com/jeantimex/ios-swift-collapsible-table-section

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let header = tableView.dequeueReusableHeaderFooterViewWithIdentifier("header") as! NovaListaTableViewHeader


    header.titleLabel.text = sections[section].name
    header.arrowLabel.text = ">"
    header.setCollapsed(sections[section].collapsed)

    header.section = section
    header.delegate = self

    return header
}

【问题讨论】:

  • 分享您的代码并分享您在 Stackoverflow 上阅读的类似问题。否则你的问题很快就会被标记。 :|

标签: ios swift uitableview


【解决方案1】:

有几种方法可以做到这一点。大多数通过编程实现,有些通过故事板和程序实现。

所以这完全取决于你想如何实现它。

我可以与您分享自定义页眉和页脚部分的最简单方法。

如果您对情节提要有很好的控制力,请尝试创建一个

UITableViewCell 

现在根据需要装饰它并在标识符中将其命名为 SectionHeader 现在将其用作重用单元

使用此委托方法后,我将共享一个目标 C 委托,

        override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {


var headerView: SectionHeaderTableViewCell? = tableView.dequeueReusableCellWithIdentifier("SectionHeader")
  if (headerView == nil) {
    headerView = SectionHeaderTableViewCell(style:UITableViewCellStyle.Subtitle, reuseIdentifier:"SectionHeader")
  }
  headerView!.textLabel!.text = "Hello World"


            return headerView;

        }

现在对页脚做同样的事情。

【讨论】:

  • 这在 Xcode 8.3.3 和 Mac OS 10.12.4 中对我不起作用
  • 你忘了提到SectionHeaderTableViewCell 应该继承UITableViewHeaderFooterView
  • 不要忘记覆盖部分中标题的方法高度,否则默认情况下您看不到任何内容,因为默认情况下返回0。所以添加类似:override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return 60 }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-03
  • 2017-10-18
  • 1970-01-01
  • 2012-02-13
相关资源
最近更新 更多