Tableview not updating correctly after adding person

Posted by tazboy on Stack Overflow See other posts from Stack Overflow or by tazboy
Published on 2012-03-19T23:09:58Z Indexed on 2012/03/19 23:30 UTC
Read the original article Hit count: 314

Filed under:
|

I have to be missing something simple here but it escapes me. After the user enters a new person to a mutable array I want to update the table. The mutable array is the datasource. I believe my issue lies within cellForRowAtIndexPath.

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

    TextFieldCell *customCell = (TextFieldCell *)[tableView dequeueReusableCellWithIdentifier:@"TextCellID"];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    if (indexPath.row == 0) {
        if (customCell == nil) {
            NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"TextFieldCell" owner:nil    options:nil];
            for (id currentObject in nibObjects) {
                if ([currentObject isKindOfClass:[TextFieldCell class]])
                    customCell = (TextFieldCell *)currentObject;
            }
        }
        customCell.nameTextField.delegate = self;
        cell = customCell;
    }

    else {
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

            cell.textLabel.text = [[self.peopleArray objectAtIndex:indexPath.row-1] name];
            NSLog(@"PERSON AT ROW %d = %@", indexPath.row-1, [[self.peopleArray objectAtIndex:indexPath.row-1] name]);
            NSLog(@"peopleArray's Size = %d", [self.peopleArray count]);
        }
    }

    return cell; 
}

When I first load the view everything is great. This is what prints:

PERSON AT ROW 0 = Melissa
peopleArray's Size = 2
PERSON AT ROW 1 = Dave
peopleArray's Size = 2

After I add someone to that array I get this:

PERSON AT ROW 1 = Dave
peopleArray's Size = 3
PERSON AT ROW 2 = Tom
peopleArray's Size = 3

When I add a second person I get:

PERSON AT ROW 2 = Tom
peopleArray's Size = 4
PERSON AT ROW 3 = Ralph
peopleArray's Size = 4

Why is not printing everyone in the array? This pattern continues and it only ever prints two people, and it's always the last two people. What the heck am I missing?

© Stack Overflow or respective owner

Related posts about iphone

Related posts about tableview