自定义uitableview单元格未显示所有文本标签

时间:2019-06-12 19:04:20

标签: ios swift uitableview firebase-realtime-database

我正在尝试在自定义单元中显示来自Firebase的对象。它具有三个文本标签。当我运行代码时,它在表格视图中每个单元格仅显示一行文本,而不是三行。它仅返回哪个文本标签是第一个。

这是我的Class对象的代码:

class Class: NSObject {
var date_clasname: String?
var teacher: String?
var room_number: String?
init(dictionary: [String: Any]) {
    self.date_clasname = dictionary["date_clasname"] as? String ?? ""
    self.teacher = dictionary["teacher"] as? String ?? ""
    self.room_number = dictionary["room_number"] as? String ?? ""
}

}

这是我的tableview类的代码:

class classes_test_TableViewController: UITableViewController {
let cellId = "cellId"

var users = [Class]()

override func viewDidLoad() {
    super.viewDidLoad()

    //navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(handleCancel))

    tableView.register(UserCell.self, forCellReuseIdentifier: cellId)

    fetchClass()
}

func fetchClass() {
   // guard let uid = ?.user.uid
       // else{return}
    //let userID = Auth.auth().currentUser!.uid
    Database.database().reference().child("Science").observe(.childAdded, with: { (snapshot) in
        //print(userID)
        if let dictionary = snapshot.value as? [String: AnyObject] {
            let user = Class(dictionary: dictionary)
            self.users.append(user)
            print(snapshot)
            //this will crash because of background thread, so lets use dispatch_async to fix
            DispatchQueue.main.async(execute: {
                self.tableView.reloadData()
            })
        }

    }, withCancel: nil)
}

@objc func handleCancel() {
    dismiss(animated: true, completion: nil)
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return users.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    // let use a hack for now, we actually need to dequeue our cells for memory efficiency
    //        let cell = UITableViewCell(style: .Subtitle, reuseIdentifier: cellId)

    let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)

    let Class = users[indexPath.row]
    cell.textLabel?.text = Class.date_clasname
    cell.textLabel?.text = Class.teacher
    cell.textLabel?.text = Class.room_number

    return cell
}

}

class UserCell: UITableViewCell {

override init(style: UITableViewCell.CellStyle, reuseIdentifier: 
String?) {
    super.init(style: .default, reuseIdentifier: reuseIdentifier)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
   }

 }

这是我的数据库结构:

"Science" : {
"-Lgxm6qJhzI2IIG4uary" : {
  "date_clasname" : "f",
  "room_number" : "d",
  "teacher" : "f"
}

该单元格应该显示所有三个字符串,但只显示一个。

1 个答案:

答案 0 :(得分:1)

您正在使用标准UITableViewCell,并将所有三个值分配给同一标签。

您必须将单元格强制转换为自定义单元格,然后将值分配给自定义标签

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    // let use a hack for now, we actually need to dequeue our cells for memory efficiency
    //        let cell = UITableViewCell(style: .Subtitle, reuseIdentifier: cellId)

    let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! UserCell

    let user = users[indexPath.row]
    cell.nameLabel?.text = user.date_clasname
    cell.teacherLabel?.text = user.teacher
    cell.roomLabel?.text = user.room_number

    return cell
}

用真实属性名称替换nameLabelteacherLabelroomLabel

请遵循命名约定和名称变量 lowerCamelCased ,例如dateClasnameroomNumber