내 데이터베이스의 게시물 수를 확인하고 tableView의 numberOfRows로 반환하고 싶습니다. 그러나 아래 코드는 작동하지 않습니다. 매번 1을 반환합니다. closure 내부에 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
}
당신의 필요 아래와 같이 변수
이제 numberOfRowsInSection 선언 전망. – rmaddy