【问题标题】:Put UIView above UITableView with section将 UIView 放在 UITableView 上方,并带有部分
【发布时间】:2016-12-02 10:12:57
【问题描述】:

我想在带有 Sections 的 UITableView 上方放置一个 UIView。

 override func viewDidLoad() {
    super.viewDidLoad()
    tableView.register(ActivityCell.self, forCellReuseIdentifier: "cellId")
    //tableView?.contentInset = UIEdgeInsetsMake(70, 0, 0, 0)
    objectArray = [Objects(sectionName: "sec 1", sectionObjects: ["sdqdsq", "sdsqdqsd", "dsqdsqd", "dsqqsdds"]),Objects(sectionName: "sec 2", sectionObjects: ["sdqdsq", "sdsqdqsd", "dsqdsqd", "dsqqsdds"]),Objects(sectionName: "sec 3", sectionObjects: ["sdqdsq", "sdsqdqsd", "dsqdsqd", "dsqqsdds"])]
    setupProfilBar()
}

let profilBar: profilMenu = {
    let pb = profilMenu()
    pb.translatesAutoresizingMaskIntoConstraints = false
    return pb
}()

func setupProfilBar() {
    view.addSubview(profilBar)
    profilBar.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
    profilBar.heightAnchor.constraint(equalToConstant: 64).isActive = true
    profilBar.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true

}

我该如何解决这个问题?谢谢

【问题讨论】:

  • 将自定义 UIView 添加到您的 ViewController 的视图中,并在其中添加 UITableView,并从 ViewController 调用bringSubviewToFront。
  • 你想让你的 UIView 高于 UITableView 吗?
  • 您想要这个仅用于第一部分还是用于所有部分?
  • @Ishika 是的,我想要
  • 首先,您需要使用 UIViewController 而不是 UITableviewController。使用 Mainstoryboard,将您的配置文件视图(即配置文件栏)放在 tableview 上方,应该是它。使用节的委托方法根据需要设置表格视图

标签: ios iphone swift uitableview


【解决方案1】:

还有一个比 cmets 中提到的更简单的方法:

您可以设置UITableViewtableHeaderView 属性。这是整个表格上方的标题。 (不是章节标题!)。

将此代码添加到您的viewDidLoad():

let v = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 200))
v.backgroundColor = UIColor.red  
tableView.tableHeaderView = v

结果如下:

【讨论】:

    【解决方案2】:

    您需要将自定义视图设置为表格视图的 tableHeaderView 属性。没有必要像其他人提到的那样使用 UIViewController 而不是 UITableViewController 。 Here's the relevant Apple Documentation.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-03
      • 2011-03-19
      • 1970-01-01
      • 2016-02-04
      • 2017-01-16
      • 2013-04-25
      相关资源
      最近更新 更多