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

标签 ios swift uitableview firebase-realtime-database

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

这是我的类对象的代码:

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"
}

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

最佳答案

您正在使用标准的 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

关于ios - 自定义 uitableview 单元格未显示所有文本标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56568837/

相关文章:

ios - 如何在 swift 3 的其他功能中使用服务器响应和数据

iphone - 为什么我的 UITableView 在异步重新加载数据期间滚动时会失去流动性?

ios - UITableViewCell 的动态维度可以,但是如果为空如何隐藏它们?

ios - UiTableView 标题不会消失

iphone - 即使应用程序在后台,多任务应用程序是否可以在设备解锁屏幕时收到通知?

iOS:快速向 UICollectionView 添加一个不可见的行

ios - 在用 Swift 编写的 Xcode 中的 iOS 应用程序中的 UITextView 中,将 UIButton 放置在文本的末尾

swift - 将@State 值委托(delegate)给子组件

ios - 为什么列表在 Swift 的另一个函数中使用时会变空?

iphone - 文本滚动到 UITextView 框边界之外