ios - 在 UITableView 的自定义单元格中使用 slider

标签 ios swift uitableview uislider

我有一个 UITableView,它有原型(prototype) cells,其中包括一个 slider 和一个 textfield 旁边。随着 slider 值的变化,textview 中的值也会发生变化。它工作正常,除了当表的大小增加时,向下滚动时生成的新 cell 已经显示第一个单元格的值。我确实意识到这与 dequeueReusableCellWithIdentifier 函数有关,因为 cells 被重用。如何规避这种情况?

这是代码:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = self.tableView.dequeueReusableCellWithIdentifier("infoCell") as! InfoCell

    cell.dressLabel.text = inputArrayFortableView[indexPath.row]

    cell.uiSlider.maximumValue = 100
    cell.uiSlider.minimumValue = 0
    cell.uiSlider.continuous = true
    cell.uiSlider.addTarget(cell, action: #selector(cell.uiSliderSetValue), forControlEvents: .ValueChanged)

    if(cell.DressTextField.text != "")
        {
            cell.uiSlider.setValue(Float(cell.DressTextField.text!)!, animated: true)
        }

    print("outside uisliderSetvalue")
    cell.uiSlider.tag = indexPath.row
    print(cell.uiSlider.tag)

    return cell

}

我附上截图。请记住,当我向下滚动时,新单元格的值与第一个单元格的值相同,生成的第二个单元格的值与第二个单元格的值相同,依此类推。

enter image description here

最佳答案

由于单元格被重复使用, slider 将保留上次使用单元格时的值。确保始终在 cellForRowAtIndexPath 中初始化 slider :

if cell.DressTextField.text != ""
{
    cell.uiSlider.setValue(Float(cell.DressTextField.text!)!, animated: true)
} else {
    cell.uiSlider.setValue(0, animated: false)
}

关于ios - 在 UITableView 的自定义单元格中使用 slider ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39545552/

相关文章:

ios - NSFetchedResultsController 不显示来自不同上下文的更新

swift - 如何修复带有无效签名的dylib?

iphone - 更改 MoreNavigationController 中的文本颜色

ios - 如何等待重复的 CAAnimation 在删除它之前完成一个循环

ios - didLoadFromCCB : come from in Cocos2d v3? 方法在哪里

ios - 核心数据类的重新声明无效

swift - 转到下一个 ViewController 并在条件有效时传递数据

iphone - 是否可以在 UITableView 上使用其他 UIControl?

ios - 强制 UITableView 调用 cellForRowAtIndexPath : for all cells

ios - 数据未显示在 UITableView 的自定义单元格中