gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 00:23:08 25 4
gpt4 key购买 nike

我遇到以下情况:

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

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

slider 类.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 的屏幕截图。 Screenshot

最佳答案

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

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

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

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 目标的 Action 名称与函数名称相同!由于它需要一个参数,所以不要忘记末尾的冒号。

所以只是为了总结一下,这里有一个非常简单的例子:

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:如何在动态 UITableView 的自定义单元格中获取 slider 的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35176104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com