自定义单元格没有出现而且没有显示来自firebase数据库的数据有问题 - 不确定哪个是问题,但我只在tableview中获得了一堆非自定义单元格而不是标题和说明。
我试图看到数据丢了打印而没有出现,我的代码有问题?调试器没有显示任何特殊内容。
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
}
}
答案 0 :(得分:0)
您有两个名为" posts。"外部,对象范围一,永远不会被设置。删除内部的。