I use a table for editing the attributes of a model object (the annotation instance). The cells in the table correspond to the different attributes of the Annotation class:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellAnnotation"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:@"cellAnnotation"] autorelease];
}
[[cell textLabel] setText:@"Category"];
categoryField = [[UITextField alloc] initWithFrame:CGRectMake(90, 8, 200, 25)];
[categoryField setAutocorrectionType:UITextAutocorrectionTypeNo];
[categoryField setFont:[UIFont fontWithName:@"Verdana" size:15]];
[categoryField setText:[[annotation category] name]];
[categoryField setDelegate:self];
[[cell contentView] addSubview:categoryField];
[categoryField release];
return cell;
Surprisingly, if I reload the data in the viewWillAppear method of the controller, then I get a weird behavior: The characters in the UITextField won’t be cleared when pressing the backspace key, although they are not there when resigning first responder in the UITextField and retrieving its text property. Can anyone help me? Thanks in advance.
You shouldn’t add subviews to a table cell outside of the if (cell == nil) { … } clause. Table cells are re-cycled so every time cellForRowAtIndex is called you are adding that subview to your table cell again without removing the old one. That may be why it looks like the text isn’t being cleared, because you’re adding multiple fields into the cell on top of one another.
Check out this other answer I wrote for a more detailed explanation:
cellForRowAtIndexPath memory management