我在自定义tableview单元格中有一个UITextField。当用户按下单元格(或UITextField)时,应该显示选择器视图而不是键盘....我使用文本字段" inputview"和" accesoryview"完成此任务的属性。
好。因此,当我选择单元格时,pickerview会显示其选择器值....但是当我选择文本字段本身(在单元格内)时,选择器视图会显示但选择器值不会显示...我只在选择器视图中看到一个空单元格。
我应该使用什么方法,以便用户按下文本字段还是单元格,同样的事情发生(也就是说,选择器视图显示选择器值)。关闭顶部是否可能导致此问题?
UITextfield in TableViewCell. Show UIPickerView
我已从代码中删除了所有混乱,并保留了与问题相关的部分(我希望我没有删除相关代码):
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
}
}
答案 0 :(得分:2)
您在didSelect中设置了选择器的数据源,您需要将其添加到textfield委托此行 myValues = diets
// UITextField Delegates
func textFieldDidBeginEditing(_ textField: UITextField) {
myValues = diets // be addedd
generalPicker()
}