【问题标题】:UITextField not editable and not showing keyboardUITextField 不可编辑且不显示键盘
【发布时间】:2018-02-15 00:06:10
【问题描述】:

我正在使用表格视图控制器来显示内容。我正在尝试制作一个可编辑的 UITextField,但它不可编辑。我可以让它们显示在每个单元格中,但是当我点击它们时,它们是不可编辑的。我还试图存储用户在每个单元格中输入的内容。请忽略注释掉的部分。这是我的代码:

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return words.count
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "transportCell", for: indexPath)

    self.tableView.rowHeight = 100



    //var a = Array(words[indexPath.row])

    //a.shuffle()

    //var shuffledWord = String(a)


    //shuffledWord = shuffledWord.lowercased()

    let sampleTextField =  UITextField(frame: CGRect(x:60, y: 30, width: 150, height: 40))
    sampleTextField.placeholder = "Enter text here"
    sampleTextField.font = UIFont.systemFont(ofSize: 15)
    sampleTextField.borderStyle = UITextBorderStyle.roundedRect
    sampleTextField.autocorrectionType = UITextAutocorrectionType.no
    sampleTextField.keyboardType = UIKeyboardType.default
    sampleTextField.returnKeyType = UIReturnKeyType.done
    sampleTextField.clearButtonMode = UITextFieldViewMode.whileEditing;
    sampleTextField.contentVerticalAlignment = UIControlContentVerticalAlignment.center
    sampleTextField.delegate = self as? UITextFieldDelegate
    var userEntry = sampleTextField.text
    sampleTextField.tag = indexPath.item // You will access the text field with this value
    cell.addSubview(sampleTextField)


    //cell.textLabel?.text = shuffledWord

    //var imageName = UIImage(named: words[indexPath.row])
    //cell.imageView?.image = imageName

    return cell
}

【问题讨论】:

  • 您似乎没有遵循您在your previous question 上收到的任何好的建议。
  • sampleTextField.isUserInteractionEnabled = true
  • 如果我包含完整代码而不只是这部分代码会有帮助吗?其他地方可能有问题。

标签: ios swift uitableview uitextfield


【解决方案1】:

cell.addSubview(sampleTextField) 更改为cell.contentView.addSubview(sampleTextField)

contentView详情请参考https://developer.apple.com/documentation/uikit/uitableviewcell/1623229-contentview

【讨论】:

    【解决方案2】:

    文本字段不可编辑可能有几个原因。

    1. 检查您是否使用了 UITextFieldDelegate 并将 textfield 委托设置为 self 即 textfield.delegate = self

    2. 当您将子视图添加到单元格时,请确保将其添加到单元格的内容视图中。例如 cell.contentView.addSubview(textField)

    3. 确保文本字段顶部没有其他视图。

    4. 如果你已经实现了:

    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool 那么它应该返回 true 否则键盘将不会出现并且你将无法编辑文本字段。

    1. 检查是否同时启用了文本字段查看快速文件和情节提要。

    6.确保您的故事板或 Xib 文件中的文本字段与您声明文本字段的视图控制器中的@IBOutlet 文本字段之间建立了正确的连接。

    1. 请检查您没有退出键盘或在 textFieldShouldBeginEditing、shouldChangeCharactersIn 和 textFieldShouldBeginEditing 的 textfield 代表上将 endEditing 设置为 true。

    希望对你有所帮助!!

    【讨论】:

    • 我尝试这样做,但没有成功。如果我包含更多代码会有帮助吗?
    【解决方案3】:

    据我所知,

    1. 您需要在选择表格视图单元格时设置 sampleTextField 的优先级。 每当您尝试选择 sampleTextField 进行编辑时, tableview 的 didselect 函数正在调用。通过放置检查 didselect 函数中的断点。如果发生这种情况,您需要 设置优先级。
    2. 另外,请检查您是否没有在键盘委托功能中的 textFieldShouldEndEditing 中退出键盘。
    3. sampleTextField.isUserInteractionEnabled = true
    4. 另外,将 cell.contentView 上的 sampleTextField 直接添加到单元格中。

    曾经, 您可以编辑,您可以直接从文本字段中获取数据并通过 sampleTextField.text 进行存储

    希望,这将是有效的。如果您仍然发现问题,请随时提出。

    【讨论】:

    • 我尝试这样做,但没有成功。如果我包含更多代码会有帮助吗?
    猜你喜欢
    • 2020-12-31
    • 1970-01-01
    • 2020-03-02
    • 1970-01-01
    • 1970-01-01
    • 2011-08-21
    • 1970-01-01
    • 2014-03-16
    • 1970-01-01
    相关资源
    最近更新 更多