我试图用自定义cell
填充我的tableView
,但每当我运行我的应用程序时,tableView
总是显示为空。我的代码有什么问题吗?我检查了标识符,一切都正确。我还检查了所有变量,看看它们是否为 nil,但没有一个是 nil。
import UIKit
import Firebase
class StatisticsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var userArray : [UserString] = [UserString]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
tableView.delegate = self
tableView.dataSource = self
tableView.register(UINib(nibName: "DataCell", bundle: nil), forCellReuseIdentifier: "customMessageCell")
configureTableView()
loadInformation()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return userArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customMessageCell", for: indexPath) as! CustomTableViewCell
cell.heightLabel.text = "Height - " + userArray[indexPath.row].height
cell.weightLabel.text = "Weight - " + userArray[indexPath.row].weight
return cell
}
func loadInformation(){
let messageDB = Database.database().reference().child("Users")
messageDB.observe(.childAdded) { (snapshot) in
let snapshotValue = snapshot.value as! Dictionary<String,String>
let email = snapshotValue["UserEmail"]!
if (email == Auth.auth().currentUser?.email as String?){
let user : UserString = UserString()
user.name = snapshotValue["UserName"]!
user.height = snapshotValue["UserHeight"]! + " cm"
user.weight = snapshotValue["UserWeight"]! + " kg"
user.date = snapshotValue["EntryDate"]!
self.userArray.append(user)
self.configureTableView()
self.tableView.reloadData()
}
}
}
func configureTableView(){
tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = 120.0
}
}
最佳答案
尝试使用静态高度,我认为您的单元格中存在与约束相关的问题,这就是为什么UITableViewAutomaticDimension
不起作用尝试使用静态高度,如果它有效,然后查看单元格约束。
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100.0
}
关于ios - 我的 TableView 未填充我的自定义单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57195396/