为什么多个UISliders同时移动?

时间:2015-12-17 05:33:09

标签: ios swift

我有一个表格视图,我在其中显示一系列项目。每个项目都有一个标签和一个滑块。有多个部分,每个部分都有几个滑块。当我拖动一个滑块时,滑块6的位置向下移动到相同的位置,而另一个6个位置之后也是如此,重复直到没有更多的滑块。为什么会发生这种情况?如何阻止它?

表格单元格的类

 class CharItemCell: UITableViewCell {
     @IBOutlet weak var charItemLabel: UILabel!

     @IBOutlet weak var charItemSlider: UISlider!
     var charItemId = Int()
     func configureForCharItem(charItem: CharItem) {
         charItemLabel.text = charItem.title
         charItemId = charItem.id
         charItemSlider.tag = charItem.id        
     }

}

表设置代码

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return charSections.count
}

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return charSections[section].title
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return charSections[section].charItems.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(TableView.CellIdentifiers.CharItemCell, forIndexPath: indexPath) as! CharItemCell
    cell.configureForCharItem(charSections[indexPath.section].charItems[indexPath.row])
    let myBackView=UIView(frame:cell.frame)
    cell.selectedBackgroundView = myBackView
    cell.tag = cell.charItemId


    return cell
}
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let returnedView = UIView(frame: CGRectMake(0, 0, tableView.frame.width, tableView.frame.height)) //set these values as necessary

    returnedView.backgroundColor = UIColorFromHex(0x253850, alpha: 1)
    let label = UILabel(frame: CGRectMake(20, 0, tableView.frame.width, 28))
    label.text = charSections[section].title
    label.textColor = UIColorFromHex(0xe8e8e8, alpha: 1)
    label.font = UIFont(name: label.font.fontName, size: 14)
    returnedView.addSubview(label)

    return returnedView
}

示例数据

class CharSection {
    let title: String
    let charItems: Array<CharItem>
    init(title: String, charItems: Array<CharItem>) {
        self.title = title
        self.charItems = charItems
    }
    class func charSections() -> Array<CharSection> {
        return [
            CharSection(title: "QUALITY", charItems: quality()),
            CharSection(title: "LITERATURE/FICTION", charItems: fiction())
        ]
    }
    class func quality() -> Array<CharItem> {
        return [
            CharItem(title: "Overall", value:0.0,id: 1)

        ]
    }
    class func fiction() -> Array<CharItem> {
        return [
            CharItem(title: "Action", value:0.0,id: 2),
            CharItem(title: "Creativity", value:0.0,id: 3)
        ]
    }

}

1 个答案:

答案 0 :(得分:1)

I think you should save slider value in your model classes because when you scroll then cell which is not shown will be removed and another cell wil be shown,
so when you scroll to previous cell that's UISlider should have value previous value.
Save value on sliderChanged and insert that in cellForRowAtIndexPath.

请点击以下链接:

Uislider in a tableView Cell