gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:02:31 25 4
gpt4 key购买 nike

当我从 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)
}
}

View Controller

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)
}

}

最佳答案

你需要像这样实现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
}
}

关于swift - 如何从 UIPickerView 更新自定义 TableViewCell 中的 textfield.text 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37643787/

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