0
我試圖根據其位置對用戶進行分組。例如,紐約市的任何人都應該在紐約市中心的桌面視圖下,任何在洛杉磯的人都應該在該欄目下。UiTableView以編程方式按部分整理項目
//Sets up the sections
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
//Sets the header view
guard let header = view as? UITableViewHeaderFooterView
else {
return
}
//Sets the properties.
view.tintColor = ChatMessageCell.indexedColor
header.textLabel?.textColor = UIColor.white
header.textLabel?.font = UIFont(name: "Open Sans Bold", size: 11)
header.backgroundColor = ChatMessageCell.indexedColor
header.textLabel?.textAlignment = .center
//Sets the variable headerText = to the text labels header
self.headerText = header.textLabel?.text!
}
我創建了一個名爲headerText的變量,用於存儲標題的文本標籤。 在numberOfRowsInSection中,我將用戶的位置與標題標題進行比較。如果他們匹配,我回報用戶,但如果他們不這樣做,沒有什麼應該返回
//Sets the number of rows equal to the amount of Users.
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//Observes the users (Firebase
refHandle = ref.child("Users").observe(.childAdded, with: { (snapshot) in
if let dictionary = snapshot.value as? [String : AnyObject]{
let user = Users()
user.id = snapshot.key
user.setValuesForKeys(dictionary)
//If statement to see if their location is equal to header it should be under
//This array is empty + meant to store only the users whose location is equal to the section title
if user.location == self.headerText{
return user
}
else{
return 0
}
}
})
}