表格单元格中的标签未显示

问题描述

我正在用一个单元格创建一个表视图,并从Firebase检索数据以显示标签firstName。正在检索数据(我已经使用print语句进行了检查),但是即使alpha设置为1,标签也没有显示

这是我的代码

import FirebaseDatabase
import FirebaseStorage
import FirebaseAuth
import SwiftKeychainWrapper

class userTableViewController: UITableViewController {

    var profileData = [profileStruct]()
    let storageRef = Storage.storage().reference()
    var databaseRef = Database.database().reference()
    var ref: DatabaseReference?
    var firstName = ""
    var lastName = ""
    var email = ""
    var phoneNumber = ""
    
     override func viewDidLoad() {
         super.viewDidLoad()
         getProfile()
         // Do any additional setup after loading the view.
      //   tableView.register(profileCell.self,forCellReuseIdentifier: "ProfileCell")
     }

     override func didReceiveMemoryWarning() {
         super.didReceiveMemoryWarning()
         // dispose of any resources that can be recreated.
     }
     
     
     struct profileStruct {
         let email : String!
         let phoneNumber : String!
         let firstName : String!
         let lastName : String!
     }
     
    @IBAction func signOut(_ sender: Any) {
        KeychainWrapper.standard.removeObject(forKey: "uid")
        do {
            try Auth.auth().signOut()
        } catch let signOutError as NSError {
            print ("Error signing out: %@",signOutError)
        }
        dismiss(animated: true,completion: nil)
    }
    
    func getProfile() {
        let databaseRef = Database.database().reference()
        databaseRef.child("users").queryOrderedByKey().observeSingleEvent(of: .childAdded,with: {
                    snapshot in
            self.firstName = ((snapshot.value as? NSDictionary)!["firstname"] as? String)!
            self.lastName = ((snapshot.value as? NSDictionary)!["lastname"] as? String)!
            self.email = ((snapshot.value as? NSDictionary)!["email"] as? String)!
            self.phoneNumber = ((snapshot.value as? NSDictionary)!["phone number"] as? String)!
            print(self.firstName)
                    dispatchQueue.main.async {
                        self.tableView.reloadData()
                    }
                })
    }
    

     override func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
         return 1
     }
    
    
    override func tableView(_ tableView: UITableView,heightForRowAt indexPath: IndexPath) -> CGFloat {
      //  tableView.dequeueReusableCell(withIdentifier: "PostCell")!.frame.size.height
        return 500
    }
     
    override func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
         guard let cell = tableView.dequeueReusableCell(withIdentifier: "ProfileCell") as? profileCell else { return UITableViewCell() }
         cell.firstNameLabel?.text = "first name: " + firstName
         cell.lastNameLabel?.text = "last name: " + lastName
         cell.emailLabel?.text = "email: " + email
         cell.phoneNumberLabel?.text = "phone number: " + phoneNumber
         return cell
     }

}

解决方法

  1. guard let cell = tableView.dequeueReusableCell(withIdentifier: "ProfileCell") as? profileCell else { return UITableViewCell() }上添加一个断点,您需要检查as? profileCell是否成功,并检查是否Data Source被调用。
  2. 如果第一步正常,则需要检查profileCell UI布局。打开Debug View Hierarchy,然后检查tableView上是否有任何单元格。
,
func getProfile() {
let databaseRef = Database.database().reference()
databaseRef.child("users").queryOrderedByKey().observeSingleEvent(of: .childAdded,with: {
            snapshot in
    self.firstName = ((snapshot.value as? NSDictionary)!["firstname"] as? String)!
    self.lastName = ((snapshot.value as? NSDictionary)!["lastname"] as? String)!
    self.email = ((snapshot.value as? NSDictionary)!["email"] as? String)!
    self.phoneNumber = ((snapshot.value as? NSDictionary)!["phone number"] as? String)!
    print(self.firstName)
self.tableView.dataSource = self
self.tableView.delegate = self
    self.tableView.reloadData()
 })}
,

您似乎在这里显示电子邮件而不是名字。

cell.emailLabel?.text = "email: " + firstName
cell.phoneNumberLabel?.text = "phone number: " + lastName
cell.firstNameLabel?.text = "first name: " + email
cell.lastNameLabel?.text = "last name: " + phoneNumber