保存从Firebase数据库Swift加载的TableView上的选中标记

时间:2018-05-21 03:17:08

标签: ios swift

所以我从firebase数据库填充表View。我可以添加和删除复选标记。但我似乎无法弄清楚如何保存它。由于tableView每次出现视图时都会重新加载数据。

这是我的视图控制器

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纳克

3 个答案:

答案 0 :(得分:0)

  1. 将您的模型更改为此
  2. 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. 即使重新加载屏幕

    4. ,也会保留复选标记

答案 1 :(得分:0)

根据Moaz Khan更改模型类,还需要使用以下代码更改cellForRowAt

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 {
        cell.accessoryType = .checkmark
    }else {
        cell.accessoryType = .none
    }
    return guestListTextCell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let isChecked = !self.guestListText[indexPath.row].isChecked
    let key = self.guestListText[indexPath.row].key
    Checkedservice.checkuncheck(key: key, isChecked: isChecked) { (seccess) in
        guard seccess else { return }
        self.guestListText[indexPath.section].isChecked = isChecked
        self.tableView.reloadRows(at: [indexPath], with: .none)
    }
}

为firebase检查标记服务

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)
        }
    }
}

更新模型类

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

       }
    }

答案 2 :(得分:0)

谢谢!我继续实施了iParesh,但我认为我的问题就在这里。因为我正在使用childByAutoID。

QML

this is the issue i'm having