ios - Swift 以编程方式设置文本字段的占位符

标签 ios arrays uitableview textfield placeholder

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

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

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/

相关文章:

objective-c - TableView 不允许字典中出现 'section'

ios - 共享扩展程序未出现在能够在 iPhone 上共享照片的应用程序列表中

iphone - 在单击表格单元格时推送新 View ,这怎么可能?

ios - 空闲连接 - 超时 - XMPP 连接

ios - 在 Swift 3.0 中将 UIButton 分层到 UILabel 上是否合适?

c++ - 如何在不创建实例的情况下获取std::array <T,N>中的元素数?

arrays - Perl 脚本问题

javascript - 将 PHP 对象数组转换为 javascript 数组

ios - 构建聊天应用程序时应该使用 UITableView 还是 UICollectionView?

Swift UITableView Realm 删除后顺序错误