以编程方式快速设置Textfield的占位符

时间:2022-08-09 17:03:22

I'm currently trying to figure out how to set the placeholder text of a textfield. The text field is within a cell. The text I would like to assign to the placeholder is contained in an array.

我目前正在试图弄清楚如何设置文本字段的占位符文本。文本字段位于单元格内。我想分配给占位符的文本包含在一个数组中。

I previously did a similar thing with setting images, and that looked like this

我之前做过类似的设置图像,看起来像这样

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

So, using that ideology I thought using...

所以,使用那种我认为使用的意识形态......

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

... would work. However, I get the error "Value of type ‘UITableViewCell’ has no member 'textField'"

... 会工作。但是,我收到错误“类型的值'UITableViewCell'没有成员'textField'”

2 个解决方案

#1


0  

Try this...

尝试这个...

If u have a textField on a custom cell, then add this following code inside cellForRowAt function.

如果您在自定义单元格上有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
    }

#2


0  

The error "Value of type ‘UITableViewCell’ has no member 'textField'" says it all. Either you have used a prototype cell that doesn't know anything about your TextField or you have used your custom cell but haven't changed it's class to your CustomCellClass. Besides, you may have casted your reusable cell as UITableViewCell other than your CustomCellClass.

错误“类型'UITableViewCell'的值没有成员'textField'”说明了一切。您使用的原型单元格对TextField一无所知,或者您已使用自定义单元格但尚未将其类更改为CustomCellClass。此外,您可能已将可重用单元格转换为除CustomCellClass之外的UITableViewCell。

Fix these issues, your code is okay.

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

#1


0  

Try this...

尝试这个...

If u have a textField on a custom cell, then add this following code inside cellForRowAt function.

如果您在自定义单元格上有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
    }

#2


0  

The error "Value of type ‘UITableViewCell’ has no member 'textField'" says it all. Either you have used a prototype cell that doesn't know anything about your TextField or you have used your custom cell but haven't changed it's class to your CustomCellClass. Besides, you may have casted your reusable cell as UITableViewCell other than your CustomCellClass.

错误“类型'UITableViewCell'的值没有成员'textField'”说明了一切。您使用的原型单元格对TextField一无所知,或者您已使用自定义单元格但尚未将其类更改为CustomCellClass。此外,您可能已将可重用单元格转换为除CustomCellClass之外的UITableViewCell。

Fix these issues, your code is okay.

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