我在 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/