所以我从 firebase 数据库填充 TableView 。我可以添加和删除复选标记。但我似乎无法弄清楚如何保存它。由于 tableView 每次 View 出现时都会重新加载数据。
这是我的 View Controller
import UIKit
import FirebaseDatabase
import Firebase
class guestListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var guestListTableView: UITableView!
var guestListDBRef : DatabaseReference!
var guestListText = [AdminTextModel]()
var keyArray : [String] = []
override func viewDidLoad() {
super.viewDidLoad()
guestListDBRef = Database.database().reference().child("RSVP")
guestListDBRef.queryOrdered(byChild: "name").observe(DataEventType.value, with: {(snapshot) in
if snapshot.childrenCount > 0 {
for guestListLabel in snapshot.children.allObjects as! [DataSnapshot] {
let guestListTextObject = guestListLabel.value as? [String: AnyObject]
let name = guestListTextObject?["name"]
let date = guestListTextObject?["date"]
let guestListTextLabels = AdminTextModel(name: name as! String?, date: date as! String?)
self.guestListText.append(guestListTextLabels)
self.guestListTableView.rowHeight = 45
self.guestListTableView.reloadData()
self.getKeys()
}
}
})
// Do any additional setup after loading the view.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return guestListText.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let guestListTextCell = tableView.dequeueReusableCell(withIdentifier: "guestList") as! guestListTableViewCell
let text: AdminTextModel
text = guestListText[indexPath.row]
guestListTextCell.guestListNameLabel.text = text.name
guestListTextCell.guestListDateLabel.text = text.date
return guestListTextCell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if tableView.cellForRow(at: indexPath)?.accessoryType == UITableViewCellAccessoryType.checkmark {
tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.none
} else {
tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.checkmark
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
我的管理文本模型
import Foundation
class AdminTextModel {
var name: String?
var date: String?
init(name: String?, date: String?) {
self.name = name
self.date = date
}
}
还有我的 TableViewCell
import UIKit
class guestListTableViewCell: UITableViewCell {
@IBOutlet weak var guestListDateLabel: UILabel!
@IBOutlet weak var guestListNameLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
如果您有任何意见,请告诉我!
enter image description here
enter image description here
enter image description here吴
Best Answer-推荐答案 strong>
- 把你的模型改成这个
class AdminTextModel {
var name: String?
var date: String?
var isChecked: Bool?
init(name: String?, date: String?, isChecked: Bool?) {
self.name = name
self.date = date
self.isChecked = isChecked
}
}
- isChecked 属性也应保存在您的 firebase 数据库中
现在获取数据并根据 isChecked 属性将复选标记设置为 true 或 false。
这将在重新加载屏幕后保留复选标记
关于ios - 在从 Firebase 数据库 Swift 加载的 TableView 上保存复选标记,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/50441534/
|