2017-04-20 92 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

修复这些问题,你的代码没问题。

相关问题