ios - UITextField 不可编辑且不显示键盘

标签 ios swift uitableview uitextfield

我正在使用 TableView Controller 来显示内容。我正在尝试制作一个可编辑的 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
}

最佳答案

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

请引用https://developer.apple.com/documentation/uikit/uitableviewcell/1623229-contentview有关 contentView

的详细信息

关于ios - UITextField 不可编辑且不显示键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48798239/

相关文章:

iphone - UITableViewCell 不断被取消选择

iphone - didSelectRowAtIndexPath:未被调用

ios - 如何在 MPMediaItemArtwork 中加载 URL 图片

ios - 如何在 Swift 4 中通过 POST 请求发送图像?

ios - 跟踪点击到应用安装?

linux - 在 Linux 上桥接到/从 CoreFoundation

ios - 使用通用 URL 方案向 Google 导航添加多个站点

ios - Deinit 在 vi​​ewControllers 上实现它是一个好习惯吗?

ios - Swift 单元测试 - 我如何断言 CGColor 是它应该是什么?

uitableview - 使用 Storyboard的快速动态表格 View 单元格高度