I am building a custom cell in the storyboard with the identifier “TextCell”. It is a plane cell, however by code i am adding to each cell a textfield because i can have an “n” number of cells so the user can insert text in each. My issue is that the textfields that i create seem to be creating themselves over and over again in top of the other. I can say this because i have a placeholder text and it gets darker and darker.
I am using ARC as well. Please any insight that you can provide me can help me a lot.
I am attaching the code of the function where i am adding the textfields to the cells:
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"TextCell"];
//create text field inside cell and init it with the default settings
UITextField * textHolder = [[UITextField alloc] initWithFrame:CGRectMake(18, 10, 300, 44)];
[textHolder setPlaceholder:[cellsText objectAtIndex:indexPath.row]];
//[textHolder setValue:[UIColor blackColor] forKeyPath:@"_placeholderLabel.textColor"];
[textHolder setFont:[UIFont fontWithName:@"HelveticaNeue-Regular" size:16]];
[textHolder setTextColor:[UIColor blackColor]];
[textHolder setTextAlignment:UITextAlignmentLeft];
textHolder.tag = FIELD_PREFIX + indexPath.row;
[textHolder setDelegate:self];
[[NSBundle mainBundle] loadNibNamed:@"accessoryView" owner:self options:nil];
[textHolder setInputAccessoryView:accessoryView];
[cell addSubview:textHolder];
//set keyboard to type passwords
[textHolder setSecureTextEntry:YES];
Each time the cell gets reused you add another
UITextFieldto the preexisting set of text fields. You have two options:1) Put the text field in the custon table view cell in your storyboard instead of creating it manually. By using a custom subclass of
UITableViewCellyou can as well assign your text field to a property in your custom subclass. This has the added bonus that you may add a bunch of other things to the cell. You can then access the property instead of creating a new text field.2) Remove the “old” text field before creating a new one. You’d probably have to search all subviews for instances of
UITextFieldto achieve this as you are already using the tag value for something else.The first one is definitely the cleaner version.