我目前正在尝试弄清楚如何设置文本字段的占位符文本。文本字段位于单元格内。我想分配给占位符的文本包含在一个数组中。
我之前在设置图像时做过类似的事情,看起来像这样
cell?.imageView?.image = row1images[indexPath.row]
所以,使用我认为使用的意识形态......
cell?.textField?.text = nil
cell?.textField?.placeholder = row1[indexPath.row]
... 会起作用。但是,我收到错误消息“‘UITableViewCell’类型的值没有成员‘textField’”
最佳答案
试试这个...
如果您在自定义单元格上有一个 textField,则将以下代码添加到 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
}
关于ios - Swift 以编程方式设置文本字段的占位符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43508498/