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?
—UPDATED—
Ok. My cells are not updating properly and I figured what I asked advice for was going to help me. I guess that wasn’t the main issue.
My issue is that my rows are not printing the proper information. When the view first loads I get:
Melissa
Dave
but after I add Tom, I get:
Melissa Dave
Melissa or Dave
Tom Tom
and after I add Ralph, I get:
Melissa ?
Tom or ?
Tom Tom
Ralph Ralph
What is going on?
The problem is that you are not updating the label of the cells that are reused. You are setting it only once when the cell is newly created. So Change your code into: