First and last UITableViewCell keep changing while scrolling.

Posted by W Dyson on Stack Overflow See other posts from Stack Overflow or by W Dyson
Published on 2011-01-08T20:02:28Z Indexed on 2011/01/08 20:54 UTC
Read the original article Hit count: 224

I have a tableView with cells containing one UITextField as a subview for each cell. My problem is that when I scroll down, the text in the first cell is duplicated in the last cell. I can't for the life if me figure out why. I have tried loading the cells from different nibs, having the textFields as ivars. The UITextFields don't seem to be the problem, I'm thinking it has something to do with the tableView reusing the cells.

The textFields all have a data source that keeps track of the text within the textField and the text is reset each time the cell is shown.

Any ideas?

UPDATE: Thanks guys, here's a sample:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

NSLog(@"Section %i, Row %i", indexPath.section, indexPath.row);

static NSString *JournalCellIdentifier = @"JournalCellIdentifier";

UITableViewCell *cell = (UITableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:JournalCellIdentifier];

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:JournalCellIdentifier] autorelease];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryNone;

}

if (indexPath.section == 0) {
    UITextField *textField = (UITextField *)[self.authorCell viewWithTag:1];
    [cell addSubview:textField];
    self.authorTextField = textField;
    self.authorTextField.text = [self.textFieldDictionary objectForKey:@"author"];
    NSLog(@"Reading Author:%@", [self.textFieldDictionary objectForKey:@"author"]);

}

else if (indexPath.section == 1) {
    UITextField *textField = (UITextField *)[self.yearCell viewWithTag:1];
    [cell addSubview:textField];
    self.yearTextField = textField;
    self.yearTextField.text = [self.textFieldDictionary objectForKey:@"year"];
    NSLog(@"Reading Year:%@", [self.textFieldDictionary objectForKey:@"year"]);     
}

else if (indexPath.section == 2) {
    UITextField *textField = (UITextField *)[self.volumeCell viewWithTag:1];
    [cell addSubview:textField];
    self.volumeTextField = textField;
    self.volumeTextField.text = [self.textFieldDictionary objectForKey:@"volume"];
    NSLog(@"Reading Volume:%@", [self.textFieldDictionary objectForKey:@"volume"]);     
}

return cell;

}

© Stack Overflow or respective owner

Related posts about iphone

Related posts about cocoa-touch