2017-10-15 60 views
0

我有一個可擴展的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

Photo from the front

在光標前的視圖是placeholderField並且不阻斷光標因爲光標仍然不當它位於placeholderField下方時出現。

回答

1

首先,我不知道你的代碼是如何工作的,因爲沒有編譯它,我可以看到幾個主要問題。

  • 你在哪裏返回CellcellForRowAt indexPath
  • cell.teacherNameField是不是有在StudentTableViewCell

坦率地說,這些東西都不是很難,他們的方式你想代碼,只要保持它的簡單,並認爲編寫代碼(每個可能的方法),在此之前只有你會達到最佳做法。

現在回到你的問題,我試着用你的代碼,並越來越重疊,那你是不是能夠做到這一點,那麼你是誰可以解決這個問題的最佳人選的事情。

使用Debug View Hierarchy

enter image description here

然後你就可以在屏幕上看到的每一個運行時視圖層。

enter image description here

+0

對不起我的代碼。我評論了很多,看看這些屬性是否改變了事情,並且我意外地刪除了一些本意的內容。我已經用我現在擁有的一切更新了我的代碼。我還添加了2張照片,說明如何在Debug View Hierarchy中實際看到光標。我還打印出遊標的描述,出於某種原因,它的alpha是0.是否有任何方法可以將alpha從0更改爲1以使其可見。再次感謝您的幫助,我會在編寫其他內容時記住這些建議。 –