简体   繁体   English

iOS Swift Textfield在Tableview内部

[英]iOS Swift Textfield Inside Tableview

I'm having three textfields inside tableview. 我在tableview中有三个textfields I need to set range for each textfield like: 我需要为每个textfield设置范围,例如:

TextField1 -> MobileNumber -> I not allow user to type more than 10 digit TextField1 > MobileNumber->我不允许用户键入超过10位的数字

Textfield2 -> PostalCode -> I not allow user to type more than 6 digit Textfield2 > Textfield2 ->我不允许用户输入超过6位数字

Textfield3 -> UserName -> I not allow user to leave first character as empty Textfield3 >用户名->我不允许用户将第一个字符保留为空

Assuming that the 3 text fields are in the same cell: 假设3个文本字段位于同一单元格中:

Create 3 different UITextField s in the UI Builder and put them inside a cell in the table view. 在UI Builder中创建3个不同的UITextField ,然后将它们放在表视图的单元格中。

Click on the first text field and from the attributes inspector set its tag property to 1. Set the tag property of the 2 other text fields to 2 and 3. 单击第一个文本字段,然后在属性检查器中将其标记属性设置为1。将其他2个文本字段的标记属性设置为2和3。

  • tag 1 for mobile number text field. 手机号码文本字段的标签1。
  • tag 2 for postal code text field. 邮政编码文本字段的标签2。
  • tag 3 for user name text field. 用户名文本字段的标签3。

Now, in your cellForRowAtIndex method and at the index of the cell that contains the 3 text fields: 现在,在您的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
}

You can achieve the same result by subclassing UITableViewCell and making the 3 text fields properties in it. 通过子类化UITableViewCell并在其中创建3个文本字段属性,可以实现相同的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM