I intended to display data from my Firestore Database into my app using the TableViewCell. It supposed to show the bookTitle, bookAuthor and bookSummary but the bookAuthor does not show up. Below is my code.
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
}
2
Answers
You’re setting the same label to
bookAuthor
andbookSummary
here:So, it gets set to
bookAuthor
, and then immediately overwritten bybookSummary
.You could create/use another label for one of the fields, or concatenate the strings for this single label:
As You had used textLabel and detailTextLabel: It’s used for giving label and a subtitle. You can’t use detailTextLabel twice. So its overriding your bookAuthor by bookSummary.
Please try to write like this:-