2017-03-02 90 views
0

在我的应用程序中,我定制了UITableViewCell,并且在定制单元格中有UIStepper和UILabel。我不知道如何检查哪个步进器被点击。那么是否知道点击哪个单元格步进器?我使用SWIFT 3.如何将uiStepper添加到uitableviewcell?

我想这

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 
let data = ["111111", "2222222","3333333"] 

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
{ 
return(data.count) 
} 
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
{ 

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell 
    cell.lb1.text = data[indexPath.row] 
    cell.stepper.tag = indexPath.row 
    cell.stepper.addTarget(self, action: #selector(stepperA(sender:)), for: .valueChanged) 
    return (cell) 

} 
func stepperA (sender: UIStepper){ 


    print("stepper \(sender.tag) clicked. Its value \(sender.value)") 
} 











override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 


} 

回答

1

您可以使用标签。这里是一个例子cellForRowAt和你的步进动作功能。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell: StepperTableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell") as! StepperTableViewCell 

    //stepper is your UIStepper reference from StepperTableViewCell 
    cell.stepper.tag = indexPath.row 
    cell.stepper.addTarget(self, action: #selector(stepperAction(sender:)), for: .valueChanged) 

    return cell 
} 
func stepperAction(sender: UIStepper) { 
    print("Stepper \(sender.tag) clicked. Its value \(sender.value)") 
} 

,其输出

Stepper 0 clicked. Its value 1.0 
Stepper 1 clicked. Its value 1.0 
Stepper 0 clicked. Its value 2.0 
Stepper 0 clicked. Its value 3.0 
Stepper 3 clicked. Its value 1.0 
+0

谢谢你,我迅速是新和我说我的代码我qustion但它不工作 –

+0

你得到任何错误? – theduman

+0

当我点击Stepper时,应用程序关闭,您可以制作一个简单的应用程序,并将其开放给我,这样我就可以检查我的错误 –

相关问题