ios - 可重用单元不是从原型(prototype)单元创建的?

标签 ios swift uitableview firebase firebase-realtime-database

我正在创建一个 iOS 应用程序,它从 Firebase 数据库加载数据,然后使用该信息创建一个 UI TableView 。我创建了一个带有重用标识符“cell”的原型(prototype)单元以及一个带有标签的自定义单元类。但是,当应用程序加载时,创建的单元格没有我为原型(prototype)单元格创建的样式,即使可重用单元格具有与原型(prototype)单元格相同的标识符。我无法更改原型(prototype)单元格的标签文本,加载应用程序时我得到的只是具有默认单元格高度的空白行,而不是我添加到原型(prototype)单元格的自定义项。

这是 UITableView 文件的代码

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() }
                    }
                }
            }
        }

}

这是我为自定义单元格创建的类:

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
    }

}

但是,当我将可重用单元格属性更改为

cell.textLabel?.text = contentOfCellPost.post_words

我可以在表格 View 中看到文本,但它仍然不是基于 Storyboard中的原型(prototype)单元规范。

image of table view

最佳答案

你错过了实现 heightForRowAt 或者如果你愿意使用动态 tableView 单元格高度

关于ios - 可重用单元不是从原型(prototype)单元创建的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48053203/

相关文章:

ios - iOS 中的数字格式问题

ios - 在 swift 3 中以编程方式设置 UIImageView AspectRatio 约束

objective-c - 使用 NSDictionary,您可以确定值的数据类型吗?

ios - 将类中的字符串元素附加到字符串数组

快速编程 NSErrorPointer 错误等

swift - 如果无法访问所有这些,则继续

ios - 删除 WithCompletionHandler 导致退出游戏中心

ios - UITableView 背景色 iOS 9

ios - 如何在uitableview didselect方法中将多个值存储在单个变量中

ios - 更改 UItableView 高度后,我的滚动不再起作用。无法到达底部数据