自定义表格视图单元格中的 UITextField。点击单元格/文本字段时显示选择器视图

Posted

技术标签:

【中文标题】自定义表格视图单元格中的 UITextField。点击单元格/文本字段时显示选择器视图【英文标题】:UITextField in custom tableview cell. Show pickerview when cell/textfield is tapped 【发布时间】:2018-05-16 16:34:36 【问题描述】:

我在自定义表格视图单元格中有一个 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

【问题讨论】:

【参考方案1】:

您在 didSelect 中为选择器设置了数据源,您需要将其添加到 textfield 委托这一行 myValues = Diets

// UITextField Delegates
    func textFieldDidBeginEditing(_ textField: UITextField) 
         myValues = diets  // be addedd
         generalPicker()
    

【讨论】:

如此明显......谢谢......我赞成你的回答,但不幸的是,因为我是新来的,它没有显示......希望其他人会赞成 有没有办法确定正在编辑的文本字段属于哪个单元格?例如找出这个特定的文本字段属于第三个单元格?... 是的,使用简单的解决方案设置文本字段标签来索引路径行

以上是关于自定义表格视图单元格中的 UITextField。点击单元格/文本字段时显示选择器视图的主要内容,如果未能解决你的问题,请参考以下文章

当点击自定义 TableView 单元格中的 UITextField 时转到新视图

在 UIViewController 的自定义表格单元格中的 UITextField 上成为 NextResponder

快速滚动ios时自定义单元格中的UITextField值出现问题

在自定义单元格中聚焦 UITextfield

根据在单元格中的文本字段中输入的值动态增加表格视图单元格

自定义表格单元格中的堆栈视图