我想檢查我的數據庫中的帖子數,並將其作爲我的tableView中的numberOfRows返回。但是,下面的代碼不起作用。它每次返回1。我知道這是因爲我在關閉中設置了var requestPostCount,但我不確定如何解決它。在關閉之外返回一個變量
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var requestPostcount: Int = 1
if segmentOutlet.selectedSegmentIndex == 0 {
// This is temporary
return 1
}
else {
// Query the database to check how many posts are there
ref.child("Request Posts").observe(.value, with: { (snapshot) in
var requestPostCount = Int(snapshot.childrenCount)
// If database is empty, only 1 row is needed to display error message
if requestPostCount == 0 {
requestPostCount = 1
}
})
}
return requestPostcount
}
你需要在加載表之前加載數據視圖。 – rmaddy