我只有一个月的时间来学习Swift和编程。所以请忍受。
我正在尝试更改由ChildbyAutoID创建的Child内的child值。我要更改的值是isChecked值。但是它所做的一切都会创建一个新的Child并在其中设置其值。
我的视图控制器
import UIKit
import FirebaseDatabase
import Firebase
class guestListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var guestListTableView: UITableView!
var guestListDBRef : DatabaseReference!
var guestListText = [AdminTextModel]()
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 isChecked = guestListTextObject?["isChecked"]
let key = guestListTextObject?["keyID"]
let guestListTextLabels = AdminTextModel(key: key as! String?, name: name as! String?, date: date as! String?, isChecked: isChecked as! Bool? )
self.guestListText.append(guestListTextLabels)
self.guestListTableView.rowHeight = 45
self.guestListTableView.reloadData()
}
}
})
// 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
if text.isChecked! {
GuestListTextCell.accessoryType = .checkmark
}else {
GuestListTextCell.accessoryType = .none
}
return GuestListTextCell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let isChecked = self.guestListText[indexPath.row].isChecked!
self.guestListText[indexPath.row].isChecked! = true
let key = self.guestListText[indexPath.row].key
Checkedservice.checkuncheck(key: key!, isChecked: isChecked) { (seccess) in
guard seccess else { return }
self.guestListText[indexPath.row].isChecked = true
self.guestListTableView.reloadRows(at: [indexPath], with: .none)
print("\(isChecked)")
print(key!)
}
}
struct Checkedservice {
static func checkuncheck(key: String, isChecked: Bool, success: @escaping (Bool) -> Void) {
let onlinesRef = Database.database().reference().child("RSVP").child(key).child("isChecked")
onlinesRef.setValue(isChecked) {(error, _ ) in
if let error = error {
assertionFailure(error.localizedDescription)
success(false)
}
success(true)
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
我的自定义文字模型
进口基金会
class AdminTextModel {
var key: String?
var name: String?
var date: String?
var isChecked: Bool?
init(key: String?, name: String?, date: String?, isChecked: Bool?) {
self.key = key
self.name = name
self.date = date
self.isChecked = isChecked
}
}
我的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
}
}
这就是我写数据库的方式。
@IBAction func rsvpButton(_ sender: Any) {
let key = rsvpRef?.child("RSVP").childByAutoId().key
let guestList = [
"keyID": key,
"name": nameTextField.text!,
"date": dateTextField.text!,
"isChecked": false] as [String : Any]
rsvpRef?.child("RSVP").childByAutoId().setValue(guestList)
if (nameTextField.text == nil) || dateTextField.text == nil {
print("No RSVP Entered")
self.dateTextField.text = nil
self.nameTextField.text = nil
}
dateTextField.text = ""
nameTextField.text = ""
}
我的最终目标是检查和取消检查单元格。但是当我重新加载视图时,选中和未选中的单元格仍然存在。非常感谢!
我的firebase结构
最佳答案
@IBAction func rsvpButton(_ sender: Any)
{
// Get first ID of your record
let databaseRef = Database.database().reference(withPath: "RSVP").observe(.childAdded)
{ (snapshot:DataSnapshot) in
// Here you will get your ID now Update value
print(snapshot.key)
//here is your value
let guestList = [
"keyID": key,
"name": nameTextField.text!,
"date": dateTextField.text!,
"isChecked": false] as [String : Any]
// Update your Child Valus
Database.database().reference(withPath: "RSVP").child(snapshot.key).updateChildValues(guestList)
}
}