Reputation:
I'm fairly new to Swift and I'm trying to make a tableView appear in a popup as shown here.
I have the datasource and delegate set to self and I call reloadData() after fetching data from Cloud Firestore. The thing is, numberOfRowsInSection might get called once, but can't get called again. CellForRowAt never gets called.
Does this have to do with the fact that I made my tableView programmatically? Something like it doesn't think the frame is set or something, even though it is. The table does work if I just make the table in Xcode manually and link an outlet. The sad thing is I do the same thing in a different view controller, but in that view controller it does work and I can't find any differences in the code.
Here's the function that gets called when you press the button
@IBAction func ShowTeams(_ sender: Any) {
RefreshData()
let startPoint = CGPoint(x: self.btnShowTeams.frame.origin.x + 15, y: self.btnShowTeams.frame.origin.y + 23)
tblTeams = UITableView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 180))
tblTeams.register(UITableViewCell.self, forCellReuseIdentifier: "cellTeams")
let popover = Popover()
tblTeams.rowHeight = 35
tblTeams.contentInset = UIEdgeInsets(top: 15,left: 0,bottom: 0,right: 0)
tblTeams.separatorColor = UIColor(hexFromString: "13293d")
popover.show(tblTeams, point: startPoint)
}
Here are the functions that set up the tableView
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
if tableView == tblTeams{
print("this shows like once, and yes there's data in dataTeams")
return dataTeams.count
}else{
return 0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView == tblTeams{
print("this doesnt show")
let cell = tableView.dequeueReusableCell(withIdentifier: "cellTeams", for: indexPath)
cell.textLabel?.textAlignment = .center
cell.textLabel?.font = UIFont.systemFont(ofSize: 22, weight: UIFont.Weight.bold)
cell.accessoryType = .disclosureIndicator
cell.textLabel!.text = dataTeams[indexPath.row]
return cell
}else{
let cell = tableView.dequeueReusableCell(withIdentifier: "cellInvites", for: indexPath)
return cell
}
}
Here's the fetch data function
func RefreshData(){
let db = Firestore.firestore()
let uid = Auth.auth().currentUser!.uid
dataTeams = [String]()
var i = 1
while i <= 6 {
db.collection("teams").whereField("uid\(i)", isEqualTo: uid)
.getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
for document in querySnapshot!.documents {
self.dataTeams.append((document["username"] as? String)!)
}
print("the code does always make it here, I checked")
self.tblTeams.reloadData()
}
}
i = i+1
}
}
And the stuff at the top for good measure. Thank you!
import UIKit
import Firebase
import FirebaseFirestore
import GradientLoadingBar
import SCLAlertView
import Popover
class Game: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var btnShowTeams: UIButton!
var dataTeams = [String]()
var tblTeams: UITableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
tblTeams.dataSource = self
tblTeams.delegate = self
RefreshData()
}
Upvotes: 0
Views: 194
Reputation: 40018
@IBAction func ShowTeams(_ sender: Any) {
RefreshData()
let startPoint = CGPoint(x: self.btnShowTeams.frame.origin.x + 15, y: self.btnShowTeams.frame.origin.y + 23)
tblTeams = UITableView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 180))
tblTeams.register(UITableViewCell.self, forCellReuseIdentifier: "cellTeams")
let popover = Popover()
tblTeams.rowHeight = 35
tblTeams.contentInset = UIEdgeInsets(top: 15,left: 0,bottom: 0,right: 0)
tblTeams.separatorColor = UIColor(hexFromString: "13293d")
popover.show(tblTeams, point: startPoint)
}
In the above code you're creating new table view every time you click your button. And the newly created tableview has nil
data source and delegate (by default).
So either set data source or delegate in above method
tblTeams.dataSource = self
tblTeams.delegate = self
or reuse the existing table view by replacing
tblTeams = UITableView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 180))
with
tblTeams.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 180)
Upvotes: 1