Having trouble with custom cell not appearing and not showing data from firebase database - not sure which one is the problem, but I only get a bunch of non-custom cells in the tableview instead of caption and description.
I have tried to see the data threw print and nothing appearing, something wrong with my code? debugger not showing anything special.
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
}
}
You have two data arrays named "posts." The outer, object scope one, never gets set. Remove the inner one.