3

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..

The iOSDev
  • 5,237
  • 7
  • 41
  • 78
mhmhsh
  • 161
  • 1
  • 13

2 Answers2

8

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;
}
0x141E
  • 12,613
  • 2
  • 41
  • 54
  • I fixed an error in my answer and provided additional information. Please update your code, appropriately. – 0x141E Aug 10 '12 at 20:16
  • @0x141E I accidentally down voted this answer. Are there any possible edits you could make so that I may retract this vote and revert it to an upvote? – Brian Tracy Oct 28 '14 at 01:28
1

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.

Phillip Mills
  • 30,888
  • 4
  • 42
  • 57
  • I used the section property from the passed parameter indexPath to control which cell should be considered. Is there a better way? – mhmhsh Aug 08 '12 at 03:28
  • Use a different identifier for each section and only add the extra views if it's a new cell. – Phillip Mills Aug 08 '12 at 11:19