我打算使用 TableViewCell 將 Firestore 資料庫中的資料顯示到我的應用程式中。它應該顯示 bookTitle、bookAuthor 和 bookSummary 但 bookAuthor 沒有出現。下面是我的代碼。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "cell")
cell.accessoryType = .disclosureIndicator
cell.textLabel?.text = books[indexPath.row].bookTitle
cell.textLabel?.font = .systemFont(ofSize: 20, weight: .medium)
cell.detailTextLabel?.text = books[indexPath.row].bookAuthor
cell.detailTextLabel?.text = books[indexPath.row].bookSummary
return cell
}
func setupTableView() {
view.addSubview(tableView)
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
tableView.allowsSelection = true
tableView.isUserInteractionEnabled = true
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
tableView.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor).isActive = true
tableView.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor).isActive = true
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return books.count
}

uj5u.com熱心網友回復:
您在此處設定相同bookAuthor的標簽bookSummary:
cell.detailTextLabel?.text = books[indexPath.row].bookAuthor
cell.detailTextLabel?.text = books[indexPath.row].bookSummary
因此,它被設定為bookAuthor,然后立即被 覆寫bookSummary。
您可以為其中一個欄位創建/使用另一個標簽,或連接該單個標簽的字串:
cell.detailTextLabel?.text = "\(books[indexPath.row].bookAuthor ?? "") - \(books[indexPath.row].bookSummary ?? "")"
uj5u.com熱心網友回復:
正如您使用過的 textLabel 和 detailTextLabel:它用于提供標簽和字幕。您不能使用 detailTextLabel 兩次。所以它通過 bookSummary 覆寫你的 bookAuthor。
請嘗試這樣寫:-
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "cell")
cell.accessoryType = .disclosureIndicator
cell.textLabel?.text = books[indexPath.row].bookTitle
cell.textLabel?.font = .systemFont(ofSize: 20, weight: .medium)
cell.detailTextLabel?.text = books[indexPath.row].bookAuthor "\n" books[indexPath.row].bookSummary
return cell
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/427938.html
上一篇:從概念中獲取資料庫內容
