![](/img/trans.png)
[英]Passing a variable to another view controller and showing data in a tableview
[英]TableView Not Showing on View Controller
I am trying to display a UITableView and its cells in a UIViewController but I am not seeing the UITableView appear in the UIViewController. 我的 AutoLayout 约束将表格设置为显示在“我的旅行”UILabel 下方。 我已经添加了委托和数据源,不确定我在这里缺少什么。
class TripsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
//components
let viewTitle = UILabel()
let tripsTableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
setup()
style()
layout()
}
}
extension TripsViewController {
func setup(){
tripsTableView.delegate = self
tripsTableView.dataSource = self
}
func style() {
view.backgroundColor = .systemBackground
viewTitle.translatesAutoresizingMaskIntoConstraints = false
viewTitle.text = "My Trips"
viewTitle.font = UIFont.preferredFont(forTextStyle: .title1)
tripsTableView.translatesAutoresizingMaskIntoConstraints = false
}
func layout() {
view.addSubview(viewTitle)
view.addSubview(tripsTableView)
NSLayoutConstraint.activate([
viewTitle.leadingAnchor.constraint(equalToSystemSpacingAfter: view.safeAreaLayoutGuide.leadingAnchor, multiplier: 2),
viewTitle.topAnchor.constraint(equalToSystemSpacingBelow: view.safeAreaLayoutGuide.topAnchor, multiplier: 2),
tripsTableView.leadingAnchor.constraint(equalToSystemSpacingAfter: view.safeAreaLayoutGuide.leadingAnchor, multiplier: 2),
tripsTableView.topAnchor.constraint(equalToSystemSpacingBelow: viewTitle.bottomAnchor, multiplier: 2)
])
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = "This is row \(indexPath.row)"
return cell
}
}
你在这里超级近。 问题在于你的限制。
NSLayoutConstraint.activate([
viewTitle.leadingAnchor.constraint(equalToSystemSpacingAfter: view.safeAreaLayoutGuide.leadingAnchor, multiplier: 2),
viewTitle.topAnchor.constraint(equalToSystemSpacingBelow: view.safeAreaLayoutGuide.topAnchor, multiplier: 2),
tripsTableView.leadingAnchor.constraint(equalToSystemSpacingAfter: view.safeAreaLayoutGuide.leadingAnchor, multiplier: 2),
tripsTableView.topAnchor.constraint(equalToSystemSpacingBelow: viewTitle.bottomAnchor, multiplier: 2),
tripsTableView.rightAnchor.constraint(equalToSystemSpacingAfter: self.view.rightAnchor, multiplier: 1),
tripsTableView.heightAnchor.constraint(equalToConstant: 200)
])
你可以在这里看到我添加了一个正确的锚点和 200 的高度。
表视图出现。
祝你好运
为表格视图添加尾随和底部约束:
NSLayoutConstraint.activate([
viewTitle.leadingAnchor.constraint(equalToSystemSpacingAfter: view.safeAreaLayoutGuide.leadingAnchor, multiplier: 2),
viewTitle.topAnchor.constraint(equalToSystemSpacingBelow: view.safeAreaLayoutGuide.topAnchor, multiplier: 2),
tripsTableView.leadingAnchor.constraint(equalToSystemSpacingAfter: view.safeAreaLayoutGuide.leadingAnchor, multiplier: 2),
tripsTableView.topAnchor.constraint(equalToSystemSpacingBelow: viewTitle.bottomAnchor, multiplier: 2),
// add these constraints
view.safeAreaLayoutGuide.trailingAnchor.constraint(equalToSystemSpacingAfter: tripsTableView.trailingAnchor, multiplier: 2),
view.safeAreaLayoutGuide.bottomAnchor.constraint(equalToSystemSpacingBelow: tripsTableView.bottomAnchor, multiplier: 2),
])
我需要将 UIViewController 更改为 UITableViewController 因为我在我的代码中使用 static 单元格(现在)。 我还覆盖了numberOfRowsInSection
和cellForRowAt
函数。
改变了
class TripsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
至
class TripsViewController: UITableViewController {
您还需要设置尾随和底部(或高度)约束。 它将设置正确的表格视图框架。
片段:
tripsTableView.trailingAnchor.constraint(equalToSystemSpacingAfter: view.safeAreaLayoutGuide.trailingAnchor, multiplier: 2), tripsTableView.bottomAnchor.constraint(equalToSystemSpacingBelow: view.safeAreaLayoutGuide.bottomAnchor, multiplier: 2)
注意:根据需要设置多个
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.