gpt4 book ai didi

ios - tableView.reloadData() 只调用一次

转载 作者:行者123 更新时间:2023-11-28 11:36:36 25 4
gpt4 key购买 nike

我是 Swift 的新手,我正在尝试让 tableView 出现在弹出窗口中,如此处所示。 tableView

我将数据源和委托(delegate)设置为 self,并在从 Cloud Firestore 获取数据后调用 reloadData()。问题是,numberOfRowsInSection 可能被调用一次,但不能被再次调用。 CellForRowAt 永远不会被调用。

这是否与我以编程方式创建 tableView 的事实有关?类似的东西并不认为框架已经设置好,即使它是。如果我只是在 Xcode 中手动制作表格并链接 socket ,那么表格确实有效。可悲的是,我在不同的 View Controller 中做了同样的事情,但在那个 View Controller 中它确实有效,而且我在代码中找不到任何差异。

这是按下按钮时调用的函数

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

最佳答案

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

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

所以在上面的方法中要么设置数据源要么委托(delegate)

tblTeams.dataSource = self
tblTeams.delegate = self

或通过替换来重用现有的 TableView

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)

关于ios - tableView.reloadData() 只调用一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55074351/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com