菜鸟教程小白 发表于 2022-12-11 20:11:00

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


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

<p>这是我的 ViewController </p>

<pre><code>import UIKit
import FirebaseDatabase
import Firebase

class guestListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var guestListTableView: UITableView!



var guestListDBRef : DatabaseReference!
var guestListText = ()

var keyArray : = []
override func viewDidLoad() {
    super.viewDidLoad()
    guestListDBRef = Database.database().reference().child(&#34;RSVP&#34;)
    guestListDBRef.queryOrdered(byChild: &#34;name&#34;).observe(DataEventType.value, with: {(snapshot) in
      if snapshot.childrenCount &gt; 0 {
      for guestListLabel in snapshot.children.allObjects as! {
            let guestListTextObject = guestListLabel.value as?
            let name = guestListTextObject?[&#34;name&#34;]
            let date = guestListTextObject?[&#34;date&#34;]
            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) -&gt; Int {
    return guestListText.count

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
    let guestListTextCell = tableView.dequeueReusableCell(withIdentifier: &#34;guestList&#34;) as! guestListTableViewCell
    let text: AdminTextModel
    text = guestListText
    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.
}
*/

}
</code></pre>

<p>我的管理文本模型</p>

<pre><code>import Foundation

class AdminTextModel {
var name: String?
var date: String?
init(name: String?, date: String?) {

    self.name = name
    self.date = date
   }
}
</code></pre>

<p>还有我的 TableViewCell</p>

<pre><code>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
}

}
</code></pre>

<p>如果您有任何意见,请告诉我!
<a href="/image/OiKJL.p" rel="noreferrer noopener nofollow">enter image description here</a> </p>

<p> <a href="/image/bPkIh.png" rel="noreferrer noopener nofollow">enter image description here</a> </p>

<p> <a href="/image/a5Vo1.png" rel="noreferrer noopener nofollow">enter image description here</a>吴</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><ol>
<li>把你的模型改成这个</li>
</ol>

<blockquote>
<pre><code>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

       }
    }
</code></pre>
</blockquote>

<ol 开始=“2”>
<li>isChecked 属性也应保存在您的 firebase 数据库中</li>
<li><p>现在获取数据并根据 isChecked 属性将复选标记设置为 true 或 false。 </p></li>
<li><p>这将在重新加载屏幕后保留复选标记</p></li>
</ol></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 在从 Firebase 数据库 Swift 加载的 TableView 上保存复选标记,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/50441534/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/50441534/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 在从 Firebase 数据库 Swift 加载的 TableView 上保存复选标记