简体   繁体   English

tableView.reloadData()仅调用一次

[英]tableView.reloadData() only called once

I'm fairly new to Swift and I'm trying to make a tableView appear in a popup as shown here. 我对Swift还是相当陌生,我试图使tableView出现在弹出窗口中,如下所示。 tableView

I have the datasource and delegate set to self and I call reloadData() after fetching data from Cloud Firestore. 我将数据源和委托设置为self,并从Cloud Firestore提取数据后调用reloadData()。 The thing is, numberOfRowsInSection might get called once, but can't get called again. 关键是,numberOfRowsInSection可能被调用一次,但无法再次调用。 CellForRowAt never gets called. CellForRowAt永远不会被调用。

Does this have to do with the fact that I made my tableView programmatically? 这是否与我以编程方式制作tableView的事实有关? 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. 如果我只是手动在Xcode中创建表格并链接插座,那么该表格就可以工作。 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 这是设置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()
}
@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). 并且新创建的tableview具有nil数据源和委托(默认情况下)。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM