2017-03-02 97 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時,應用程序關閉,您可以製作一個簡單的應用程序,並將其開放給我,這樣我就可以檢查我的錯誤 –

相關問題