ios - 我的 TableView 未填充我的自定义单元格

标签 ios swift xcode

我试图用自定义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/

相关文章:

ios - Apple ResearchKit : how to extract information from ORKResult? 第一次程序员

c++ - 堆栈和队列在调试器中显示为空。苹果

ios - 使用来自另一个操作的现有展开转场

ios - 如何将推送通知 token 存储到外部数据库中 - iOS 10、Swift 3

c# - UIViewController 子类(模仿 UITableViewController)未发布

ios - 如何创建一个 NSManagedObjectContext 作为另一个 NSManagedObjectContext 的子集?

swift - 如何在 Swift 中使用位域来存储超过 1 位的值

ios - 由于联系委托(delegate)断言失败导致游戏崩溃

ios - 应用响应时在 Xcode-Swift 中呼吸动画

ios - 无法使用 Swift 2 在给定字符串中搜索模式