如何在每个TableViewCell中保存唯一的分数?

发布于 2025-01-18 05:43:53 字数 921 浏览 3 评论 0原文

我想将分数保存在我用领域保存的单元格中。

这是我程序的图像。

“

当我按下左上角按钮时,警报弹出以添加带有标签的新单元格。该单元格由领域保存并正确显示。然后,在单元格上,每侧都有两个按钮,可以将数字添加或减在单元格下的分数中。

现在,当我更改每个单元格时,我也想保存分数,并在可能的情况下使用领域。

这是我要保存它的地方:

class CellVC: UITableViewCell {
    var scorepts: Int = 0
    let realm = try! Realm()
    var infosScore: Results<ScoreInfos>?

    @IBAction func minusPressed(_ sender: Any) {
        if scorepts != 0 {
             scorepts -= 1
        countScore.text = "\(scorepts)"
         
            // save the score of the this cell cell
        }
        
        }
    @IBAction func addPressed(_ sender: Any) {
       
        scorepts += 1
        countScore.text = "\(scorepts)"
       
        // save the score of this cell cell

    }

然后,当它保存时,我需要在视图加载时加载单元格分数。

感谢您的帮助。

I would like to save a score in a cell that I saved with Realm.

Here is an image of my program.

enter image description here

When I press the top left button, an alert pops up to add a new cell with a label. The cell is saved with realm and is displayed properly. Then on the cell, there is two buttons on each side which can add or subtract a number to the score under the label of the cell.

Now I want to save the score when I change it of each cell and if possible with realm too.

Here is where I want to save it :

class CellVC: UITableViewCell {
    var scorepts: Int = 0
    let realm = try! Realm()
    var infosScore: Results<ScoreInfos>?

    @IBAction func minusPressed(_ sender: Any) {
        if scorepts != 0 {
             scorepts -= 1
        countScore.text = "\(scorepts)"
         
            // save the score of the this cell cell
        }
        
        }
    @IBAction func addPressed(_ sender: Any) {
       
        scorepts += 1
        countScore.text = "\(scorepts)"
       
        // save the score of this cell cell

    }

Then when it's saved, I need to load the cell scores when the view loads.

Thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文