Firebase 数据库不将数据拉取到表视图中的自定义单元格

Firebase database not pulling data to custom cell in tableview

自定义单元格未出现且未显示来自 firebase 数据库的数据时遇到问题 - 不确定是哪个问题,但我在表格视图中只得到一堆 non-custom 单元格,而不是标题和描述。

我已经尝试查看打印出来的数据,但什么也没有出现,我的代码有问题吗?调试器没有显示任何特殊内容。

import UIKit
import FirebaseDatabase

class SearchViewController: UIViewController, UISearchBarDelegate {
    @IBOutlet weak var tableView: UITableView!

    //part of load posts
    var posts = [Post]()
    func loadPosts(){
        var posts = [Post]()
        Database.database().reference().child("Posts").observe(.childAdded) {(snapshot: DataSnapshot) in
            if let dict = snapshot.value as? [String: Any] {
                let captionText = dict["caption"] as! String
                let descriptionText = dict["description"] as! String
                let photoUrlString = dict["photoUrl"] as! String
                let tagsText = dict["tags"] as! String
                let post = Post(captionText: captionText, descriptionText: descriptionText, photoUrlString: photoUrlString, tagsText: tagsText)
                posts.append(post)
                print(self.posts)
                self.tableView.reloadData()
            }
        }
    }

    //part of SearchBar
    let searchBar = UISearchBar()

    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
        searchBar.endEditing(true)
    }
    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        searchBar.endEditing(true)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        createSearchBar()
        tableView.dataSource = self
        tableView.delegate = self
        loadPosts()
    }
    func createSearchBar(){        
        searchBar.showsCancelButton = false
        searchBar.placeholder = "Enter Here"
        searchBar.delegate = self

        self.navigationItem.titleView = searchBar
    }
}

//part of tableView
extension SearchViewController: UITableViewDataSource, UITableViewDelegate {    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return posts.count
    }    

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCell(withIdentifier:"searchCell", for: indexPath)
        as! CustomTableViewCell
        cell.titleField?.text = posts[indexPath.row].caption
        //cell.descriptionField?.text = posts.description
        cell.tagsField?.text = posts[indexPath.row].tags
        return cell
    }
}

您有两个名为 "posts." 的数据数组,外部对象范围之一永远不会被设置。去掉里面的。