我有一个可扩展的tableViewCell与几个UITextFields,一个UILabel和一个UITextView。当tableView展开时,标签消失并出现UITextFields和UITextView。在两个UITextFields中都出现光标,但在UITextView中不出现光标。我尝试更改色调颜色,但没有任何东西被刮起来,但是当我选择文本(确实出现)时,文本以浅色显示,并且可见。我也尝试改变框架,但没有奏效。另外我现在将它设置为一个普通的UITextView,不改变帧以外的任何属性。 textView是使用UITableViewCell类以编程方式设置的,并且一切看起来都起作用,但是光标。光标不显示在UITextView iOS
下面是我如何设置的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
在光标前的视图是placeholderField并且不阻断光标因为光标仍然不当它位于placeholderField下方时出现。
对不起我的代码。我评论了很多,看看这些属性是否改变了事情,并且我意外地删除了一些本意的内容。我已经用我现在拥有的一切更新了我的代码。我还添加了2张照片,说明如何在Debug View Hierarchy中实际看到光标。我还打印出游标的描述,出于某种原因,它的alpha是0.是否有任何方法可以将alpha从0更改为1以使其可见。再次感谢您的帮助,我会在编写其他内容时记住这些建议。 –