2017-04-20 99 views
4

我目前正試圖弄清楚如何設置文本框的佔位符文本。文本字段位於單元格內。我想分配給佔位符的文本包含在一個數組中。Swift以編程方式設置Textfield的佔位符

我以前做過類似的事情,與設定的圖像,而且看起來像這樣

cell?.imageView?.image = row1images[indexPath.row] 

因此,使用意識形態我想用...

cell?.textField?.text = nil 
cell?.textField?.placeholder = row1[indexPath.row] 

...會工作。不過,我得到

+1

'UITableViewCell'沒有文本字段,因此錯誤。也許'cell'應該被聲明爲你的自定義單元格的實際類型。 – rmaddy

+0

您是否創建了自定義單元格?顯示'cellforrowatindexpath'的完整代碼。 –

+0

你有兩個選項來設置TextFiled佔位符:[** TextField PlaceHolder **](https://iosdevcenters.blogspot.com/2016/11/how-to-change-uitexefield-placeholder.html) –

回答

0

試試這個錯誤「類型‘的UITableViewCell’沒有成員‘文本框’價值」 ......

如果妳有一個自定義單元格一個文本框,然後裏面添加cellForRowAt這下面的代碼功能。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

      let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell", for: indexPath) as! CustomTableViewCell 

      cell .yourTextField.attributedPlaceholder = NSAttributedString(string: row1[indexPath.row], 
                     attributes: [NSForegroundColorAttributeName: UIColor.black]) 


      return cell 
    } 
+0

我得到一個錯誤讀取「使用未聲明的類型」CustomTableViewCell「 –

0

錯誤「價值型‘的UITableViewCell’沒有成員‘文本框’」說明了一切。您可能使用了一個prototype cell,它不知道任何關於您的TextField的信息,或者您使用了custom cell,但沒有將其更改爲您的CustomCellClass。此外,您可能已經鑄造了reusable cell作爲UITableViewCell而不是您的CustomCellClass

修復這些問題,你的代碼沒問題。

相關問題