不同的UITableViewCells中的UISliders在连续模式下会相互影响。

huangapple go评论64阅读模式
英文:

UISliders inside different UITableViewCells affect each other when continuous

问题

当它们的 isContinuous 值被设置为 true 时,第一个和最后一个滑块会相互影响 - 拖动其中一个会在另一个上产生相同的数值变化。以下是创建这些单元格的方法:

class CellWithSliderValues: UITableViewCell{
    @IBOutlet var slider: UISlider! = {
        let ctrl = UISlider()
        ctrl.backgroundColor = Constants.SETTINGS_CELL_COLOR
        return ctrl
    }()
    // 一些标签
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        // 布局
    }
    
    override func prepareForReuse() {
        super.prepareForReuse()
        slider.tag = -10
        minLabel.text = nil
        maxLabel.text = nil
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

internal func makeSignalLevelCell(*某些参数*) -> UITableViewCell{
    let tag: Int
    let currValue: Float
    let minValue, maxValue: Float
    switch(kind){
    // 设置所有上述的值
    }
    if (numInSection == 0){
        // 其他单元格
    } else{
        let cell = mainScreen.table.dequeueReusableCell(withIdentifier: "CellWithSliderValues") as! CellWithSliderValues
        cell.slider.maximumValue = maxValue
        cell.slider.minimumValue = minValue
        cell.slider.value = currValue
        cell.minLabel.text = Int(minValue).description
        cell.maxLabel.text = Int(maxValue).description
        cell.slider.tag = tag
        cell.slider.addTarget(self, action: #selector(sliderChange), for: .valueChanged)
        return cell
    }
}

@objc func sliderChange(sender: UISlider){
    guard let valueToAdjustNumber = HeatmapSettings.RangedValueOption(rawValue: sender.tag) else {print("非理解的滑块"); return}
    print (valueToAdjustNumber)
    let newValueGiven = sender.value
    switch(valueToAdjustNumber){
    // 检查有效性并将更改保存到另一个变量中
    }
    sender.resignFirstResponder()
    mainScreen.table.reloadData() // 因为我们需要刷新文本框中的数字
}

表中的第一个和最后一个滑块会相互影响 - 调整一个滑块的值时,另一个滑块的值也会改变。通过一些调试输出 - 请参见上面的 print (valueToAdjustNumber) 行 - 看起来 valueToAdjustNumber 在预期的滑块和受到故障影响的滑块之间交替变化。

这不是单元格重用的问题 - 我尝试了生成新的单元格而不是重用,还尝试了重用单元格但重新创建滑块,都没有帮助。

当为滑块设置 isContinuous 为 false 时,问题消失。

英文:

I have made a UITableView with some cells containing UISliders. Every UISlider has a unique tag - the number of a value it represents.

When isContinuous value is set to true for them, the first and the last sliders affect each other - dragging one of them makes the same change in value on the other.
Here is how those cells are made:

class CellWithSliderValues: UITableViewCell{
    @IBOutlet var slider: UISlider! = {
        let ctrl = UISlider()
        ctrl.backgroundColor = Constants.SETTINGS_CELL_COLOR
        return ctrl
    }()
    // some labels
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        // layout
    }
    
    override func prepareForReuse() {
        super.prepareForReuse()
        slider.tag = -10
        minLabel.text = nil
        maxLabel.text = nil
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
internal func makeSignalLevelCell(\*some args*\) -> UITableViewCell{
        let tag: Int
        let currValue: Float
        let minValue, maxValue: Float
        switch(kind){
        \\ set all the values above
        }
        if (numInSection == 0){
            // other cells
        } else{
            let cell = mainScreen.table.dequeueReusableCell(withIdentifier: "CellWithSliderValues") as! CellWithSliderValues
            cell.slider.maximumValue = maxValue
            cell.slider.minimumValue = minValue
            cell.slider.value = currValue
            cell.minLabel.text = Int(minValue).description
            cell.maxLabel.text = Int(maxValue).description
            cell.slider.tag = tag
            cell.slider.addTarget(self, action: #selector(sliderChange), for: .valueChanged)
            return cell
        }
@objc func sliderChange(sender: UISlider){
        guard let valueToAdjustNumber = HeatmapSettings.RangedValueOption(rawValue: sender.tag) else {print("Non-understood slider"); return}
        print (valueToAdjustNumber)
        let newValueGiven = sender.value
        switch(valueToAdjustNumber){
        \\check for validity and save changes to a different variable
        }
        sender.resignFirstResponder()
        mainScreen.table.reloadData() // beacuse we need to refresh the number in text box
    }

The first and the last sliders in the table affect each other - when one has its value adjusted, the other changes its value too. With some debug outputs - see the

print (valueToAdjustNumber)

line above - it looks like valueToAdjustNumber alternates between the one expected for the slider and the one affected by the glitch.

It is not the problem of cell reuse - I tried genereting new cells instead of reuse, and also reusing cells but recreating sliders, it did not help.

When .isContinuous is set to false for the sliders, the problem disappears.

答案1

得分: 0

Bug disappeared after reloading only one section at a time.

mainScreen.table.reloadData()

Is made into

mainScreen.table.reloadSections([sectionNumberToReload(value: valueToAdjustNumber)], with: .none)

英文:

Bug disappeared after reloading only one section at a time.

mainScreen.table.reloadData() 

Is made into

mainScreen.table.reloadSections([sectionNumberToReload(value: valueToAdjustNumber)], with: .none)

huangapple
  • 本文由 发表于 2023年2月9日 02:52:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75390458.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定