简体   繁体   中英

iOS Swift Textfield Inside Tableview

I'm having three textfields inside tableview. I need to set range for each textfield like:

TextField1 -> MobileNumber -> I not allow user to type more than 10 digit

Textfield2 -> PostalCode -> I not allow user to type more than 6 digit

Textfield3 -> UserName -> I not allow user to leave first character as empty

Assuming that the 3 text fields are in the same cell:

Create 3 different UITextField s in the UI Builder and put them inside a cell in the table view.

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.

  • tag 1 for mobile number text field.
  • tag 2 for postal code text field.
  • tag 3 for user name text field.

Now, in your cellForRowAtIndex method and at the index of the cell that contains the 3 text fields:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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