如何从UIPickerView更新自定义TableViewCell中的textfield.text值

时间:2016-06-05 15:48:21

标签: swift uipickerview

当我从UIPickerView中选择一些值时,我想要更新自定义UITableViewCell中的UITextFiled.text。

如果您看一下图片,我会在自定义UITableViewCell中点击UITextField并显示UIPickerView。然后我选择数字和标签“完成”按钮。但我不知道如何将选定值从UIPickerView传递到我的自定义UITableViewCell,其中是我的UITextFiled.text。

有人可以帮帮我吗?谢谢

The screenshot

自定义单元格

class JamkaTableViewCell: UITableViewCell, pickerChangedDelegate {

    @IBOutlet weak var jmenoHraceLabel: UILabel!
    @IBOutlet weak var parTextField: UITextField!

    func onPickerChanged(val: String) {
        self.parTextField.text = val
        print(self.parTextField.text)
    }
 }

查看控制器

import UIKit

protocol pickerChangedDelegate {
    func onPickerChanged(val: String)
}

class JamkaViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {


    @IBOutlet weak var vzdalenostLabel: UILabel!
    @IBOutlet weak var jmenoLabel: UILabel!
    @IBOutlet weak var parJamkyLabel: UILabel!
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var jamkaView: UIView!

    var picker: UIPickerView!
    var karta:Karta = Karta.init()
    var index = 0
    var vzdalenost:Int!
    var jmenoJamky:String = ""
    var parJamky:Int!
    var pocetKopu:[String] = []
    var hrac:Hrac!
    var delegate: pickerChangedDelegate? = nil

    override func viewDidLoad() {
        super.viewDidLoad()

        vzdalenostLabel.text = "\(self.vzdalenost)"
        jmenoLabel.text = self.jmenoJamky
        parJamkyLabel.text = "\(self.parJamky)"

        //Vypocitat pocet kopu a trestných bodů
        pocetKopu(self.parJamky)

        //Jamka view
        self.jamkaView.layer.cornerRadius = 5

    }

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

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.karta.seznamHracu.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cellIdentifier = "Cell"
        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! JamkaTableViewCell

            self.hrac = self.karta.seznamHracu[indexPath.row]

            //Vybiratko na pary
            self.picker = UIPickerView.init()
            self.picker.delegate = self
            self.picker.dataSource = self
            self.picker.backgroundColor = svetleSeda

            let pickerToolbar = UIToolbar()
            pickerToolbar.barStyle = .Default
            pickerToolbar.sizeToFit()
            pickerToolbar.tintColor = UIColor.blackColor()

            let hotovoTlacitko = UIBarButtonItem(barButtonSystemItem: .Done, target: self, action: #selector(JamkaViewController.skrytPickerView))

            pickerToolbar.setItems([hotovoTlacitko], animated: true)
            pickerToolbar.userInteractionEnabled = true

            cell.parTextField.inputView = self.picker
            cell.parTextField.inputAccessoryView = pickerToolbar
            cell.jmenoHraceLabel.text = self.hrac.jmeno

            print(cell.parTextField.text)

        return cell

    }

    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return self.pocetKopu.count
    }

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return self.pocetKopu[row]
    }

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        delegate?.onPickerChanged(self.pocetKopu[row])
        print("did select \(self.pocetKopu[row])")
    }

    func pocetKopu(parJamky: Int) -> [String] {

        let maximalniPocetKopu = (3*parJamky)

        for i in 1...maximalniPocetKopu {

            let str = String(i)
            self.pocetKopu.append(str)

        }

        //Trestne body
        let trestneBody = String(4*parJamky)
        self.pocetKopu.append(trestneBody)

        return self.pocetKopu

    }

    func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
        let pointInTable:CGPoint = textField.superview!.convertPoint(textField.frame.origin, toView:self.tableView)
        var contentOffset:CGPoint = self.tableView.contentOffset
        contentOffset.y  = pointInTable.y
        if let accessoryView = textField.inputAccessoryView {
            contentOffset.y -= accessoryView.frame.size.height
        }
        self.tableView.contentOffset = contentOffset
        return true;
    }

    func textFieldDidEndEditing(textField: UITextField) {
        self.tableView.setContentOffset(CGPointMake(0, 0), animated: true)
        scrollView.setContentOffset(CGPointMake(0, 0), animated: true)
    }

    func textFieldShouldReturn(textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }

    func skrytPickerView() -> () {
        self.view.endEditing(true)
    }

}

2 个答案:

答案 0 :(得分:1)

你需要像这样实现pickerView的didSelectRow:

//create delegate in controller that contains picker
protocol pickerChangedDelegate{
    func onPickerChanged(val: String)
}


class vc: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate{
    @IBOutlet var picker: UIPickerView!

    //your view controller ....
    //....


    var delegate: pickerChangedDelegate? = nil
    var items = ["1", "2", "3"]

    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return items.count
    }

    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        //this will send picker value through protocol to your cell
        delegate?.onPickerChanged(items[row])
    }
}



//use delegate where you want data
class customCell: UITableViewCell, pickerChangedDelegate{
    @IBOutlet weak var myText: UITextField!

    //Cell setup
    //....
    //....


    func onPickerChanged(val: String) {
        myText.text = val
    }
}

答案 1 :(得分:0)

result = range(len(board))