简体   繁体   English

滚动后重复使用单元格后如何在表格视图单元格中保持值?

[英]how to maintain the value in table view cell after the cell is reused after scrolling?

here is the .gif of my problem: http://g.recordit.co/LnslGAaWwK.gif 这是我的问题的.gif: http//g.recordit.co/LnslGAaWwK.gif

at first, I can use increment or decrement button to increase or decrease the value. 首先,我可以使用增加或减少按钮来增加或减少该值。 but after scrolling the table view to bottom and then back to the stepper I changed before, then it seems the decrement button doesn't work, the value from the counter label still remain the same 但是将表格视图滚动到底部然后回到我之前更改过的步进器之后,似乎减量按钮不起作用,计数器标签上的值仍然保持不变

but, if I tap the increment button , it works, but still giving the wrong value, it will start again from 1, it doesn't continue the value before scrolling. 但是,如果我点击“ 增加”按钮 ,它可以工作,但仍然给出错误的值,它将再次从1开始,在滚动之前不会继续该值。

I suspect the problem is in here (the complete code is below): 我怀疑问题出在这里(完整代码如下):

    override func awakeFromNib() {
        super.awakeFromNib()
        setStepper()
    }

it seems after scrolling to the bottom of table view, it seems the setStepper() in awakeFromNib() will be triggered again and it means the initial value from stepper (stepper.value = 0) , Will be back again to zero, thats maybe why If I press the increment button, it will give 1, not continuing the value before scrolling 滚动到表格视图的底部之后,似乎将再次触发awakeFromNib()setStepper() ,这意味着stepper的初始值(stepper.value = 0) ,将再次返回零,那也许为什么如果我按下增量按钮,它将显示1,而不是在滚动之前继续该值

here is the code from my table view cell 这是我的表格视图单元格中的代码

import UIKit
import KWStepper

protocol CounterDelegate{
    func incrementOrDecrementButtonDidTapped(at selectedIndexPath:IndexPath, counterValue: Int)
}

class CheckOutCell: UITableViewCell {

    var stepper: KWStepper?
    var indexPath: IndexPath?
    var delegate: CounterDelegate?

    @IBOutlet weak var counterLabel: UILabel!
    @IBOutlet weak var decrementButton: UIButton!
    @IBOutlet weak var incrementButton: UIButton!

    var productData : Product? {
        didSet {
            updateUI()
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        setStepper()
    }


    func setStepper() {

        stepper = KWStepper(decrementButton: decrementButton, incrementButton: incrementButton)
        guard let stepper = stepper else {return}

        stepper.autoRepeat = false
        stepper.wraps = false
        stepper.minimumValue = 0
        stepper.value = 0
        stepper.incrementStepValue = 1
        stepper.decrementStepValue = 1

        stepper.valueChangedCallback = { stepper in
            let stepperValue = Int(stepper.value)

            // send data to CheckoutVC
           guard let indexPath = self.indexPath else {return}
           self.delegate?.incrementOrDecrementButtonDidTapped(at: indexPath, counterValue: stepperValue)

            print(stepperValue)

        }
    }



    func updateUI() {

        guard let productData = productData else {return}
        counterLabel.text = "\(productData.quantity)"


    }


}

so how to solve this problem ? 那么如何解决这个问题呢? how to make the value continue after I scroll the table view to the bottom ? 将表格视图滚动到底部后,如何使值继续?

You need to rethink your strategy. 您需要重新考虑您的策略。

A TableView is only the visual representation of the data in its datasource and only a part of the datasource is visible at a time. TableView只是其数据源中数据的可视表示形式,并且一次仅一部分数据源可见。

So the data which is displayed by a TableViewCell should be stored in the datasource, not in the cell. 因此,TableViewCell显示的数据应该存储在数据源中,而不是存储在单元格中。

Organize your data in an Array and let the TableViews datasource point to that array. 将数据组织在一个数组中,并使TableViews数据源指向该数组。 You need to implement the methods of the UITableViewDatasource protocol ( numberOfSections , numberOfRowsInSection ). 您需要实现UITableViewDatasource协议的方法( numberOfSectionsnumberOfRowsInSection )。

Then, override cellForRowAtIndexPath where the cell is set up with the data coming from the datasource array at the index of indexPath.row . 然后,覆盖cellForRowAtIndexPath ,在该单元格中使用来自数据源数组的数据在indexPath.row的索引处设置indexPath.row

The changes made with your stepper should then be made within the datasource. 然后,应该在数据源中进行用步进器进行的更改。

Hope you get the idea. 希望你能明白。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 滚动后表格视图单元格标签更改并在其中获得错误的值该如何解决? - Table view cell label get changed after scrolling and get wrong value in it how to solve it? 关闭视图后如何保持集合视图单元格的状态? - How to maintain state of collection view cell after dismissal of view? 滚动并重新打开后,表格视图单元格才正确遮罩 - Table view cell only masks correctly after scrolling off and then back on 表格视图在滚动后显示正确的单元格高度 - Table view displays correct cell´s height after some scrolling Swift Table View单元在滚动后更改渐变颜色 - Swift Table View Cell changes gradient Color after scrolling 表格视图单元格中的倒数计时器在滚动后显示不同的值 - Countdown timer in table view cell shows different values after scrolling 表单元中的UIswitch已重用 - UIswitch in a table cell reused 如何在表格视图中选择一个单元格而不滚动它? - How to select a cell in table view without scrolling it? 刷新后保持表格单元格移除而不触及数据库中的帖子 - Maintain table cell removal after refresh without touching the post in the database 更改单元格表格视图按钮中的图像在 Swift 中检查 unheck 也面临滚动后更改的图像 - Change image in button of cell Table View Check unheck in Swift also Facing imaged Changed After Scrolling
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM