Swift: 如何获取动态UITableView 中自定义单元格中滑块的值?

Swift: How to get the value of a slider in a custom cell in a dynamic UITableView?

我遇到以下情况的问题:

我有一个带有动态单元格的 UITableView。其中,我使用自定义 UITableViewCellUISlider。如何获取滑块的值?

我创建了以下示例:(项目文件可以在这里下载:Link

SliderClass.swift

import UIKit

class SliderClass: NSObject {
    var title: String
    var subtitle: String
    var sliderMinimum: Float
    var sliderMaximum: Float

    init(title: String, subtitle: String, sliderMinimum: Float, sliderMaximum: Float) {
        self.title = title
        self.subtitle = subtitle
        self.sliderMinimum = sliderMinimum
        self.sliderMaximum = sliderMaximum

    }
}

SliderTableViewCell.swift

import UIKit

class SliderTableViewCell: UITableViewCell {

    @IBOutlet weak var cellTextLabel: UILabel!
    @IBOutlet weak var cellDetailTextLabel: UILabel!
    @IBOutlet weak var cellSlider: UISlider!

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

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }
}

TableViewController.swift

import UIKit

class TableViewController: UITableViewController {

    var slider: [SliderClass] = []

    override func viewDidLoad() {
        super.viewDidLoad()
        title = "Slider"

        let slider1 = SliderClass(title: "Title1", subtitle: "Subtitle1", sliderMinimum: 0, sliderMaximum: 100)
        let slider2 = SliderClass(title: "Title2", subtitle: "Subtitle2", sliderMinimum: 0, sliderMaximum: 200)

        slider = [slider1, slider2]
    }

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return slider.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("sliderCell", forIndexPath: indexPath) as! SliderTableViewCell

        let slider: SliderClass
        slider = self.slider[indexPath.row]

        cell.cellTextLabel.text = slider.title
        cell.cellDetailTextLabel?.text = slider.subtitle
        cell.cellSlider.minimumValue = slider.sliderMinimum
        cell.cellSlider.maximumValue = slider.sliderMaximum

        return cell
    }

    func sliderValueChange() {
     // I need help here:
     // Which slider changed to which value?

     // var slider = ...
     // var sliderValue = ...
     // NSLog("Value of slider:%@ changed to:%i", slider, sliderValue)
    }
}

这是使用上述代码创建的 UITableView 的屏幕截图。

尝试将此添加到您的 cellForRowAtIndexPath 函数中:

cell.cellSlider.tag = indexPath.row
cell.cellSlider.addTarget(self, action: "sliderValueChange:", forControlEvents: .ValueChanged)

然后在 table 函数之外添加如下内容:

func sliderValueChange(sender: UISlider) {
    // Get the sliders value
    var currentValue = Int(sender.value)
    var sliderRow = sender.tag

    // Do whatever you want with the value :)
    // And now the row of the slider!
}

确保 cellSlider 目标的动作名称与函数名称相同!由于它需要一个参数,所以不要忘记末尾的冒号。

总结一下,这是一个非常简单的上述操作示例:

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2    // 2 rows in the cell, for demo purposes
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TableViewCell

        cell.slider.tag = indexPath.row
        cell.slider.addTarget(self, action: "sliderChange:", forControlEvents: .ValueChanged)

        return UITableViewCell()
    }

    func sliderChange(sender: UISlider) {
        let currentValue = sender.value    // get slider's value
        let row = sender.tag               // get slider's row in table

        print("Slider in row \(row) has a value of \(currentValue)")
        // example output - Slider in row 1 has a value of 0.601399
    }

}

class TableViewCell: UITableViewCell {

    @IBOutlet var slider: UISlider!

}

获取自定义单元格中滑块的值。

希望这对您有所帮助,请仔细阅读 Swift 中的 GitHub 代码并参考屏幕截图

https://github.com/appcoda/expandable-table-view

另一种选择是使用 ASValueTrackingSlider。注意是写在ObjectiveC.