【问题标题】:Swift Setting Textfield's Placeholders ProgrammaticallySwift 以编程方式设置文本字段的占位符
【发布时间】:2017-09-16 10:29:08
【问题描述】:

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

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

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

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

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

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

【问题讨论】:

  • UITableViewCell 没有文本字段,因此出现错误。也许cell 应该声明为您的自定义单元格的实际类型。
  • 您是否创建了自定义单元格?显示cellforrowatindexpath的完整代码。
  • 您有两个选项来设置 TextFiled 占位符:TextField PlaceHolder

标签: ios arrays uitableview textfield placeholder


【解决方案1】:

试试这个...

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

【讨论】:

  • 我收到一条错误消息:“使用未声明的类型“CustomTableViewCell”
【解决方案2】:

错误 “'UITableViewCell' 类型的值没有成员 'textField'” 说明了一切。要么你使用了一个对你的TextField一无所知的prototype cell,要么你使用了你的custom cell,但没有将它的类更改为你的CustomCellClass。此外,您可能将您的reusable cell 转换为UITableViewCell 而不是您的CustomCellClass

解决这些问题,您的代码就可以了。

【讨论】:

    猜你喜欢
    • 2019-02-15
    • 2016-08-27
    • 2017-05-16
    • 1970-01-01
    • 2011-11-11
    • 2015-11-13
    • 2019-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多