ios - Swift 2 无法将 slider 添加到 UITableView 单元格
<p><p>我正在尝试使用 swift 在 UItableview 中创建一个 slider 单元格,它出现了,但它不起作用,我想实时显示它的值但它不起作用,我尝试使用 <code>action</code> 在 slider 本身上,我尝试更改 <code>Editing Changed</code> 方法也不起作用。</p>
<p>这是我的手 secret 码:</p>
<pre><code>import UIKit
class SliderCell: UITableViewCell {
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var maxLegendLabel: UILabel!
@IBOutlet weak var minLegendLabel: UILabel!
@IBOutlet weak var slider: UISlider!
@IBOutlet weak var answerLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
slider.userInteractionEnabled = true
slider.continuous = true
//slider.addTarget(self, action: #selector(sliderValueChanged), forControlEvents: UIControlEvents.ValueChanged)
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
@IBAction func sliderAction(sender: UISlider) {
print("slider ACTION value")
print(sender.value)
}
func sliderValueChanged(sender: UISlider) {
print("slider value")
print(sender.value)
answerLabel.text = "Your choice : " + String(sender.value)
}
func displayBlock(block: Block){
titleLabel.text = block.title
minLegendLabel.text = block.range?.legendMin
maxLegendLabel.text = block.range?.legendMax
slider.minimumValue = Float((block.range?.min)!)!
slider.maximumValue = Float((block.range?.max)!)!
slider.value = 1
}
}
</code></pre>
<p>这是我在 <code>my tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell</code> 方法中声明它的方式:</p>
<pre><code>let cell = tableView.dequeueReusableCellWithIdentifier(CurrentFormTableView.CellIdentifiers.SliderCell, forIndexPath: indexPath) as! SliderCell
cell.displayQuestion(block)
cell.selectionStyle = UITableViewCellSelectionStyle.None
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 160.0
return cell
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>在表格 View 单元格中添加:</p>
<pre><code>sliderDemo = UISlider(frame:CGRectMake(0, 0, 200,20))
var numberOfSteps : NSInteger = numbers.count - 1
sliderDemo.minimumValue = 6.5
sliderDemo.maximumValue = 4.1
sliderDemo.continuous = true
sliderDemo.value = 4.0
sliderDemo.addTarget(self, action: "sliderValueDidChange:", forControlEvents: .ValueChanged)
self.view.addSubview(sliderDemo)
func sliderValueDidChange(sender:UISlider!)
{
println("number:\(sender.value)")
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - Swift 2 无法将 slider 添加到 UITableView 单元格,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/36939954/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/36939954/
</a>
</p>
页:
[1]