Tableview 内的 iOS Swift 文本字段

标签 ios swift3 uitextfield tableview

我在 tableview 中有三个 textfields。我需要为每个 textfield 设置范围,例如:

TextField1 -> MobileNumber -> 我不允许用户键入超过 10 个数字

Textfield2 -> PostalCode -> 我不允许用户输入超过 6 位数字

Textfield3 -> 用户名 -> 我不允许用户将第一个字符留空

最佳答案

假设 3 个文本字段在同一个单元格中:

在 UI Builder 中创建 3 个不同的 UITextField,并将它们放在 TableView 的一个单元格中。

单击第一个文本字段,然后从属性检查器中将其标签属性设置为 1。将其他 2 个文本字段的标签属性设置为 2 和 3。

  • 为手机号码文本字段标记 1。
  • 邮政编码文本字段的标记 2。
  • 为用户名文本字段标记 3。

现在,在您的 cellForRowAtIndex 方法和包含 3 个文本字段的单元格的索引处:

if let mobileNumberTextField = cell.viewWithTag(1) as UITextField {
    // Customize mobileNumberTextField
}

if let postalCodeTextField = cell.viewWithTag(2) as UITextField {
    // Customize postalCodeTextField
}

if let userNameTextField = cell.viewWithTag(3) as UITextField {
    // Customize userNameTextField
}

您可以通过子类化 UITableViewCell 并在其中创建 3 个文本字段属性来实现相同的结果。

关于Tableview 内的 iOS Swift 文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44392484/

相关文章:

iphone - 像 iPhone AppStore iOS 6 详细 View 中的选项卡(详细信息、评论、相关)

ios - 如何为我的登录设置 API 请求?

ios - 以非常高的速度绘制动态形状 iOS

ios - 如何在 Mapview swift ios 中设置自定义图钉图像?

ios - Timer.scheduledTimer 在 Swift 3 中不起作用

swift - 用作具体类型的非泛型协议(protocol)不符合自身

ios - GCD dispatch_async 内部的 NativeX fetchAd 调用。 Phonegap插件开发

ios - 允许使用字符集退格

ios7 - 将 inputAccessoryView - UIToolBar 上 UITextField 上的文本镜像到 navigationController.toolbar 上 UITextField 上的文本

iphone - 为多个 UITextFields 辞职第一响应者