简体   繁体   English

Swift 中的 TableView 问题(仅空行)

[英]Problem with TableView in Swift (only empty Rows)

I got a problem with my tableview.我的表格视图有问题。 It is a tableview in a normal ViewController.它是普通 ViewController 中的 tableview。

This is my ViewController:这是我的视图控制器:

class ListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {


var tableView:UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    tableView = UITableView(frame: view.bounds, style: .plain)
    tableView.delegate = self
    tableView.dataSource = self


    let cellNib = UINib(nibName: "PostTableViewCell", bundle: nil)
    tableView.register(cellNib, forCellReuseIdentifier: "postCell")

    view.addSubview(tableView)


    var layoutGuide:UILayoutGuide!
    layoutGuide = view.safeAreaLayoutGuide

    tableView.leadingAnchor.constraint(equalTo: layoutGuide.leadingAnchor).isActive = true
    tableView.topAnchor.constraint(equalTo: layoutGuide.topAnchor).isActive = true
    tableView.trailingAnchor.constraint(equalTo: layoutGuide.trailingAnchor).isActive = true
    tableView.bottomAnchor.constraint(equalTo: layoutGuide.bottomAnchor).isActive = true



    tableView.reloadData()


}
func numberOfSections(in tableView: UITableView) -> Int {
    1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    12
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "postCell", for: indexPath) as! PostTableViewCell
    return cell
}

} }

This is my PostTableViewCell.这是我的 PostTableViewCell。 xib西布

My PostTableViewCell.我的 PostTableViewCell。 swift is empty. swift为空。

This is my result: Iphone View这是我的结果: Iphone View

Thank you!谢谢!

In both function are specting a int return and toy are only using a number在 function 中都指定了一个 int 返回,而玩具只使用了一个数字

func numberOfSections(in tableView: UITableView) -> Int { return 1 } func numberOfSections(in tableView: UITableView) -> Int { return 1 }

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 12 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 12 }

Find the solution.找到解决方案。 Just added the rowHeight刚刚添加了 rowHeight

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM