tableView.reloadData()仅调用一次

时间:2019-03-09 05:37:03

标签: ios swift uitableview

我对Swift还是很陌生,我试图使tableView出现在弹出窗口中,如下所示。 tableView

我将数据源和委托设置为self,并从Cloud Firestore提取数据后调用reloadData()。关键是,numberOfRowsInSection可能被调用一次,但无法再次调用。 CellForRowAt永远不会被调用。

这与我以编程方式制作tableView的事实有关吗?好像它不认为框架已设置,即使已设置。如果我只是手动在Xcode中创建表格并链接插座,那么该表格就可以工作。可悲的是,我在不同的视图控制器中执行相同的操作,但是在该视图控制器中它确实起作用,并且我在代码中找不到任何差异。

这是按下按钮时调用的功能

@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)
}

以下是设置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
    }
}

这是获取数据功能

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
    }
}

顶部的东西很好。谢谢!

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()
}

1 个答案:

答案 0 :(得分:2)

@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)
}

在上面的代码中,每次单击按钮都将创建新的表格视图。新创建的tableview具有nil数据源和委托(默认情况下)。

因此可以通过上述方法设置数据源或委托

tblTeams.dataSource = self
tblTeams.delegate = self

或通过替换

重用现有的表格视图
tblTeams = UITableView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 180))

tblTeams.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 180)