[英]UITextField in custom tableview cell. Show pickerview when cell/textfield is tapped
I have a UITextField in a custom tableview cell. 我在自定义tableview单元格中有一个UITextField。 when the user presses either the cell (or the UITextField), a pickerview should show instead of the keypad....i'm using the textfields "inputview" and "accesoryview" properties to accomplish this.
当用户按下任一单元格(或UITextField)时,应该显示pickerview而不是小键盘。...我正在使用文本字段“ inputview”和“ accesoryview”属性来完成此操作。
okay. 好的。 So when i select the cell, the pickerview shows up with its picker values....however when i select the textfield itself (which is within the cell), the picker view shows up but the picker values don't show up...i only see one empty cell in the picker view.
因此,当我选择单元格时,pickerview会显示其选择器值。...但是,当我选择文本字段本身(位于单元格内)时,会显示选择器视图,但不会显示选择器值。 .i在选择器视图中只能看到一个空单元格。
what approach should I use so that whether the user presses either the textfield or cell, the same exact thing happens (that is, the pickerview shows up with picker values). 我应该使用哪种方法,以便无论用户按下文本字段还是单元格,都会发生相同的事情(即pickerview显示有picker值)。 Off the top is it obvious to you what could be causing this problem?
最明显的是,这可能是导致此问题的原因?
UITextfield in TableViewCell. TableViewCell中的UITextfield。 Show UIPickerView
显示UIPickerView
I have removed all the clutter from my code and kept just the part pertaining to the question (i'm hoping i haven't omitted relevant code): 我从代码中删除了所有混乱的部分,只保留了与问题有关的部分(我希望我没有省略相关代码):
import UIKit
class EditProfileViewController: UIViewController, UITableViewDelegate,UITableViewDataSource, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate,UITextViewDelegate {
let properties = ["Name","Birthday","Diet"]
let diets:NSArray = ["Vegan","Plant-based","Vegetarian","Meat Eater"]
var userEditProfileTable : UITableView!
var reminderCells = (1...3).map{ _ in EditProfileTableViewCell()}
var toolBar : UIToolbar!
var i:Int!
var myUIPicker = UIPickerView()
var editorViewController: EditorViewController!
var myValues: NSArray!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.blue
let barHeight: CGFloat = UIApplication.shared.statusBarFrame.size.height
let displayWidth: CGFloat = self.view.frame.width
let displayHeight: CGFloat = self.view.frame.height
userEditProfileTable = UITableView(frame: CGRect(x: 0, y: barHeight, width: displayWidth, height: displayHeight - barHeight))
userEditProfileTable.delegate = self
userEditProfileTable.dataSource = self
userEditProfileTable.rowHeight = 40
userEditProfileTable.register(EditProfileTableViewCell.self, forCellReuseIdentifier: "cellId")
self.userEditProfileTable.reloadData()
self.view.addSubview(userEditProfileTable)
}
func generalPicker(){
myUIPicker.becomeFirstResponder()
self.myUIPicker.delegate = self
self.myUIPicker.dataSource = self
}
@objc func donePicker() {
reminderCells[i].valueTextField.resignFirstResponder()
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
reminderCells[indexPath.item].valueTextField.becomeFirstResponder()
i = indexPath.item
print(properties[indexPath.item])
switch properties[indexPath.item] {
case "Name":
print("jkehdfgjdhj")
case "Birthday":
print("jhkjhjh")
case "Diet":
myValues = diets
generalPicker()
print(properties[indexPath.item])
default:
print("Some other character")
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// retuen no of rows in sections
return properties.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// retuen your custom cells
print(indexPath)
let cell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath) as! EditProfileTableViewCell
cell.backgroundColor = UIColor.clear
cell.property.text = properties[indexPath.item]
cell.isUserInteractionEnabled = true
cell.valueTextField.isUserInteractionEnabled = true
cell.valueTextField.delegate = self
toolBar = UIToolbar()
toolBar.sizeToFit()
switch properties[indexPath.item]{
case "Birthday":
print("bonita")
case "Diet":
let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.plain, target: self, action: #selector(donePicker))
toolBar.setItems([doneButton], animated: false)
cell.valueTextField.inputAccessoryView = toolBar
cell.valueTextField.inputView = myUIPicker
default:
print("mamacita")
}
reminderCells[indexPath.item] = cell
return cell
}
// data method to return the number of column shown in the picker.
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
// data method to return the number of row shown in the picker.
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
print(myValues.count)
return myValues.count
}
// delegate method to return the value shown in the picker
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
print("mjkhjkhkj")
print(myValues[row])
return myValues[row] as? String
}
// delegate method called when the row was selected.
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
print("row: \(row)")
print("value: \(myValues[row])")
reminderCells[i].valueTextField.text = myValues[row] as? String
}
} }
You set data source for picker in didSelect , you need to add it to textfield delegate this line myValues = diets 您在didSelect中为选择器设置了数据源,您需要将其添加到文本字段中,委托此行myValues = Diets
// UITextField Delegates
func textFieldDidBeginEditing(_ textField: UITextField) {
myValues = diets // be addedd
generalPicker()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.