I have a UITableView that allows the user to add rows dynamically to the table. The UITableView initially starts off with 0 rows. Each row is a custom UITableViewCell, that contains two separate UITextFields: one that holds text, one that holds numbers. The UITableView has a visible area that displays 4 rows, and then scrolls as each new row is added. Whenever the user adds a new row, I add the values in the previously added row in the UITableView in a NSMutableDictionary, where the key is the value from the name field, and the value is from the number field.
I am successfully adding rows to the UITableView, and successfully storing the data into my NSMutableDictionary. My problem however, is that after adding my rows, if the user decides to scroll up or down the UITableView, all of a sudden, the data in the rows of the table that the user entered is completely off.
Here are screen shots of what I'm talking about:
What it's supposed to look like:
What it looks like when I scroll to the top:
Here is the relevant code that I am working with:
- (IBAction)addChoice:(id)sender {
if ([self.tableData count] > 0) {
NSString *name = [[(MyCustomCell *)[self.choiceTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:[self.tableData count]-1 inSection:0]] nameField] text];
NSString *number = [[(MyCustomCell *)[self.choiceTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:[self.tableData count]-1 inSection:0]] priceField] text];
if (name && price) {
[self.tableDictionary setObject:number forKey:name];
}
}
NSString *theObjectToInsert = [NSString stringWithFormat:@"Row #: %lu", (unsigned long)[self.tableData count]];
[self.tableData addObject:theObjectToInsert];
NSIndexPath *newPath=[NSIndexPath indexPathForRow:self.tableData.count-1 inSection:0];
[self.choiceTable insertRowsAtIndexPaths:@[newPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.choiceTable scrollToRowAtIndexPath:newPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
[[(MyCustomCell *)[self.choiceTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:[self.tableData count]-1 inSection:0]] nameField] becomeFirstResponder];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyCustomCell *cell = (MyCustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if ([self.tableDictionary count] > 0) {
NSArray *keys = [self.tableDictionary allKeys];
if ([indexPath row] == 0) {
cell.nameField.text = keys[indexPath.row];
cell.numberField.text = self.tableDictionary[keys[indexPath.row]];
} else if ([indexPath row] <= ([self.tableDictionary count]-1)) {
cell.nameField.text = keys[indexPath.row-1];
cell.numberField.text = self.tableDictionary[keys[indexPath.row-1]];
} else {//if the row was just added by the user, display a table with empty fields
cell.nameField.text = @"";
cell.numberField.text = @"";
}
}
[cell.contentView setBackgroundColor:[UIColor colorWithRed:0.89 green:0.36 blue:0.01 alpha:1.0]];
return cell;
}
Can anyone see what it is I'm doing wrong?