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
Wkwebview Origin Null Is Not Allowed by Access-Control-Allow-Origin
Uidevice Uniqueidentifier Deprecated - What to Do Now
How to Take a Screenshot Programmatically on Ios
How to Programmatically Get the MAC Address of an Iphone
What Are Different Types of Notifications in iOS and How to Configure Them Correctly
Iphone 6 Plus Resolution Confusion: Xcode or Apple'S Website? For Development
How to Trigger a Block After a Delay, Like -Performselector:Withobject:Afterdelay:
Change Paid App to Free But Know If User Previously Purchased It
Prevent Screen Capture in an iOS App
Ios App Submission: Missing 64-Bit Support
Detect Tap on a Button in Uitableviewcell for Uitableview Containing Multiple Sections
How to Load an Http Url With App Transport Security Enabled in iOS 9
Performselector May Cause a Leak Because Its Selector Is Unknown
How to Take a Screen Shot of a Uiview
Can You Attach a Uigesturerecognizer to Multiple Views
How to Create Dispatch Queue in Swift 3
Move Textfield When Keyboard Appears Swift
An Error Was Encountered While Running (Domain = Launchserviceserror, Code = 0)