Swift Tableview in a Uiview Not Displaying Data

Swift TableView in a UIView not displaying data

try this:

override func viewDidLoad() {
super.viewDidLoad()

// Add this
tableView.delegate = self
tableView.dataSource = self
}

UITableView Not displaying data AND not adding data

You are missing two lines in viewDidLoad():

override func viewDidLoad() {  
super.viewDidLoad()

tableView.dataSource = self // This class is the tableview's data source
tableView.delegate = self // This class is the tableview's delegate
}

TableViewCell not displaying data

  func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 0
}

You need to return at least 1 section, as a row is a child of a section

Table View not displaying cells

First of call it's not a good practice to access cell views by tags, other is not needed to reload tableView in 'viewDidAppear' method also you should call 'super.viewDidAppear(animated)' when you override 'viewDidAppear' method.

Cell identifier should be same like you set in Storyboard when you want to dequeue the cell from tableView.

TableView should return at least one section, or you can remove numberOfSections default is one.

class UpcomingTableViewController: UITableViewController {

//MARK: - Private properties
private var upcomingPosts = [upcomingPost]() //Name convention of swift for models is CamelCase so upcomingPost should be UpcomingPost
private var ref: DatabaseReference!

override func viewDidLoad() {
super.viewDidLoad()

initializeFirebaseDatabase()
fetchUpCommingPostsFromFirebase()
}

func initializeFirebaseDatabase() {
ref = Database.database().reference()
}

private func fetchUpCommingPostsFromFirebase() {
let dbref = ref.child("auctionHandler").child("upcoming")
dbref.queryOrderedByKey().observe(DataEventType.childAdded, with: { (snapshot) in
let snapshot = snapshot.value as! NSDictionary
let shoe = snapshot["name"] as! String
let colorway = snapshot["colorway"] as! String
let date = snapshot["date"] as! String
let newPost = upcomingPost(shoe: shoe, colorway: colorway, date: date)

self.upcomingPosts.insert(newPost, at: .zero)
self.tableView.reloadData()
})
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return upcomingPosts.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "upcoming", for: indexPath) as? UPComingPostsCell else { return UITableViewCell() }
cell.updateViews(withUpcomingPost: upcomingPosts[indexPath.row])
return cell
}

}

Also tableView cell can be like this:

class UPComingPostsCell: UITableViewCell {

@IBOutlet weak var shoeLabel: UILabel! //Connect labels from storyboards
@IBOutlet weak var colorWayLabel: UILabel!

func updateViews(withUpcomingPost post: UpcomingPost) {
shoeLabel.text = post.shoe
colorWayLabel.text = post.colorway
}

}

Hope this helps. :)

UITableView() not showing in UIViewController while trying to add it programmatically

From what I see in your code, you have never defined the size of your table view.

Well actually, first, you don't even add your table view as a subview. Second, even if you do that, there is another issue: navbars and stuff have their default size and position; however, table views do not. I'm almost certain that if you launch the view debugger, you will be able to see the table view in the view hierarchy, but not on the screen, because by default, it's width and height are zero. You need to either manually set the frame in your layoutSubviews() method or set up constraints on the table view in viewDidLoad() so that it's size will be larger than zero.

TL;DR: views usually do not "just" know how large they should be and where they should go.



Related Topics



Leave a reply



Submit