gpt4 book ai didi

ios - 光标未出现在 UITextView iOS 中

转载 作者:行者123 更新时间:2023-11-29 00:10:01 26 4
gpt4 key购买 nike

我有一个可扩展的 tableViewCell,其中有几个 UITextFields、一个 UILabel 和一个 UITextView。当 tableView 展开时,标签消失,UITextFields 和 UITextView 出现。在两个 UITextField 中都会出现光标,但在 UITextView 中不会出现光标。我尝试过更改色调颜色,但没有任何效果,但是当我选择文本(确实出现)时,文本会以色调颜色突出显示并且可见。我也尝试过更改框架,但没有成功。此外,我现在将其设置为常规 UITextView,除框架外没有更改任何属性。 textView 是使用 UITableViewCell 类以编程方式设置的,除了光标外一切似乎都正常。

这是我设置 UITextView 的方法:

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

cell.infoView.frame = CGRect(x: 23, y: 207, width: cell.frame.width - 36, height: cell.frame.height - 155)
cell.infoView.delegate = self
cell.infoView.text = classes[indexPath.row].info
cell.infoView.layer.cornerRadius = 5
cell.infoView.layer.masksToBounds = true

cell.placeholderField.frame = CGRect(x: 32, y: 92, width: self.view.frame.width - 38, height: 45)
if cell.infoView.text!.replacingOccurrences(of: " ", with: "") == "" {
cell.placeholderField.placeholder = "Description"
} else {
cell.placeholderField.placeholder = ""
}
cell.placeholderField.backgroundColor = UIColor.clear

cell.nameField.frame = CGRect(x: 23, y: 3, width: cell.frame.width - 70, height: 44)
cell.nameField.text = classes[indexPath.row].name
cell.nameField.delegate = self
cell.nameField.layer.cornerRadius = 5
cell.nameField.layer.masksToBounds = true
cell.nameField.borderStyle = .roundedRect

cell.teacherField.frame = CGRect(x: 23, y: 50, width: cell.frame.width - 36, height: 44)
cell.teacherField.text = classes[indexPath.row].teacher.name
cell.teacherField.delegate = self
cell.teacherField.layer.cornerRadius = 5
cell.teacherField.layer.masksToBounds = true
cell.teacherField.borderStyle = .roundedRect

cell.editButton.frame = CGRect(x: self.view.frame.width - 60, y: 15, width: 70, height: 20)
cell.editButton.addTarget(self, action: #selector(self.editInfoView), for: .touchUpInside)
cell.editButton.setTitle(cell.editButton.title(for: .normal) ?? "Edit", for: .normal)

cell.contentView.addSubview(cell.nameField)
cell.contentView.addSubview(cell.teacherField)
cell.contentView.addSubview(cell.infoView)
cell.contentView.addSubview(cell.editButton)
cell.contentView.addSubview(cell.placeholderField)

cell.textLabel?.text = classes[indexPath.row].name
cell.detailTextLabel?.text = classes[indexPath.row].teacher.name

return cell
}

这是类定义:

class StudentTableViewCell: UITableViewCell {
var infoView = UITextView()
var placeholderField = UITextField()
var nameField = UITextField()
var teacherField = UITextField()
var editButton = UIButton()
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}

override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}

有谁知道为什么没有出现光标?

在此先感谢您的帮助。

以下是选中光标的调试层次结构中的照片: Photo from the side

Photo from the front

光标前面的 View 是 placeholderField,并且不会遮挡光标,因为当光标位于 placeholderField 下方时,光标仍然不会出现。

最佳答案

首先,我不知道你的代码是如何为你工作的,因为如果不编译它,我可以看到一些主要问题,比如。

  • cellForRowAt indexPath 中返回 Cell 的位置?
  • cell.teacherNameField 不在 StudentTableViewCell

坦率地说,这些事情并不困难,它们是您尝试编写代码的方式,只要保持简单并在编写代码之前思考(每种可能的方法),那么只有您才能实现最佳实践。

现在回到你的问题,我试过你的代码库,事情越来越重叠,你无法做到这一点,所以你是解决它的最佳人选。

使用调试 View 层次结构

enter image description here

然后您可以在屏幕上看到每个运行时 View 层。

enter image description here

关于ios - 光标未出现在 UITextView iOS 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46751609/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com