I am creating an iOS application that loads data from a Firebase Database and then creates a UI Table View with the information. I have created a prototype cell with the reuse identifier "cell" and also a custom cell class with labels. However, when the app loads, the cells created do not have the style I created for the prototype cell even though the reusable cell has the same identifier as the prototype cell. I cannot change the label text of the prototype cell, and all I am getting when I load the app is blank rows with the default cell height, not the customizations I added to the prototype cell.
Here is the code for the UITableView file
import UIKit
import Firebase
import FirebaseAuth
import FirebaseDatabase
class HomeViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var ref: DatabaseReference!
var postList = [Post]()
var refHandle : UInt!
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
//let userID = Auth.auth().currentUser?.uid
ref = Database.database().reference().child("posts") //.child(userID)
tableView.register(PostTableViewCell.self, forCellReuseIdentifier: "cell")
fetchPosts()
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return postList.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! PostTableViewCell
//set cell content
let contentOfCellPost = postList[indexPath.row]
cell.title?.text = contentOfCellPost.post_words
return cell
}
func fetchPosts () {
let query = ref.queryOrdered(byChild: "timestamp").queryLimited(toFirst: 10)
query.observe(.value) { (snapshot) in
for child in snapshot.children.allObjects as! [DataSnapshot] {
if let value = child.value as? NSDictionary {
let post = Post()
let poster = value["poster"] as? String ?? "Name not found"
let post_content = value["post"] as? String ?? "Content not found"
post.post_words = post_content
post.poster = poster
self.postList.append(post)
DispatchQueue.main.async { self.tableView.reloadData() }
}
}
}
}
}
and here is the class I created for the custom cell :
import UIKit
class PostTableViewCell: UITableViewCell {
@IBOutlet weak var title: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
However, when I change the reusable cell property to
cell.textLabel?.text = contentOfCellPost.post_words
I can see the text in the table view, however it is still not based off my prototype cell specifications in storyboard.
您想实现heightForRowAt错过了,或者如果愿意,可以使用动态tableView单元格的高度
Your problem is this line:
tableView.register(PostTableViewCell.self, forCellReuseIdentifier: "cell")
If you register a class for a table view then when you dequeue it you get a blank cell of that class not one from a storyboard. So nothing (IBOutlet, IBAction, etc) will be setup.
If you just want to dequeue the prototype cell defined in a storyboard (or XIB file) then you don't need to register it as well.
在使用情节提要时,请从viewDidLoad函数中删除以下行。
tableView.register(PostTableViewCell.self, forCellReuseIdentifier: "cell")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.