Reputation: 161
I have ageTextField
property in my TableViewController
. I add it to the first section cell's subciew but I don't know why it is shown in another section (#3) when I scroll down..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
// Configure the cell...
switch (indexPath.section){
case 0:
{
[cell addSubview:self.ageTextField];
}
break;
case 1:
{
cell.textLabel.text = [screeningArray objectAtIndex:indexPath.row];
if (indexPath.row == 0) //no choice
cell.accessoryType = self.doneScreening ? UITableViewCellAccessoryNone:UITableViewCellAccessoryCheckmark;
else //yes choice
cell.accessoryType = self.doneScreening ? UITableViewCellAccessoryCheckmark:UITableViewCellAccessoryNone;
}
break;
case 2:
{
cell.textLabel.text = @"1 : ";
[cell addSubview:self.riskTextField];
}
break;
case 3:
{
cell.textLabel.text = [findingsArray objectAtIndex:indexPath.row];
cell.accessoryType = [[boolValuesArray objectAtIndex:indexPath.row] boolValue] ? UITableViewCellAccessoryCheckmark:UITableViewCellAccessoryNone;
}
break;
default:
break;
}
cell.textLabel.font = [UIFont systemFontOfSize:14];
return cell;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSUInteger newLength = [textField.text length] + [string length] - range.length;
if (textField == self.ageTextField)
return (newLength > 2) ? NO : YES;
else
return (newLength > 7) ? NO : YES;
}
Here is screenshot at the beginning..
http://dl.dropbox.com/u/30838210/Screen%20Shot%202012-08-07%20at%209.26.47%20PM.png
After scrolling down, notice how the placeholder of ageTextField was duplicated in Section3, cell3..
http://dl.dropbox.com/u/30838210/Screen%20Shot%202012-08-07%20at%209.27.28%20PM.png
When scrolling up again, the text of cell3 in section3 appears in the first cell..
dl.dropbox.com/u/30838210/Screen%20Shot%202012-08-07%20at%209.27.52%20PM.png
That's weird and I couldn't figure out what happened! please help..
Upvotes: 3
Views: 1179
Reputation: 12753
The contents of a table view cell must be removed if they are reused (i.e, if cell != nil). Also, the subviews should be added to the table cell's contentView not to the cell itself, so the subviews adjust appropriately when entering/exiting editing mode.
[cell.contentView addSubview:self.ageTextField];
[cell.contentView addSubview:self.riskTextField];
The following code prepares the cell for reuse.
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
else
{
// Prepare cell for reuse
// Remove subviews from cell's contentView
for (UIView *view in cell.contentView.subviews)
{
// Remove only the appropriate views
if ([view isKindOfClass:[UITextField class]])
{
[view removeFromSuperview];
}
}
cell.accessoryType = UITableViewCellAccessoryNone;
cell.textLabel.text = nil;
}
Upvotes: 8
Reputation: 31016
When the table gives you a reusable cell it doesn't care which section it was previously used in. Since you add a subview whether it's a new cell or one that previously had a subview added, it's quite possible to get more than one and of various kinds.
Upvotes: 1