gpt4 book ai didi

ios - 在从 Firebase 数据库 Swift 加载的 TableView 上保存复选标记

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:02:53 25 4
gpt4 key购买 nike

所以我从 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.
}
*/

}

我的 AdminTextModel

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

最佳答案

  1. 将你的模型改成这个
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

}
}
  1. isChecked 属性也应该保存在您的 firebase 数据库中
  2. 现在获取数据并根据 isChecked 属性将复选标记设置为 true 或 false。

  3. 即使在重新加载屏幕后,这也会保留复选标记

关于ios - 在从 Firebase 数据库 Swift 加载的 TableView 上保存复选标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50441534/

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