Note that there are some explanatory texts on larger screens.

plurals
  1. POUITableView Inserting Old Data
    text
    copied!<p>I have a simple UITableView with 1 section, within which an arbitrary amount of rows with custom UITableViewCells (of type <code>KMInputCell</code>) with UITextFields exist. When a user starts typing in the last (blank) text field, a new blank row is inserted (so the user can make a list-like structure). Since I will only "save" the data when the view is closed, the data source is just an <code>NSUInteger</code> keeping track of the number of rows.</p> <p>My code works fine until after a user has deleted a row from the table. Then, when the user starts typing at the end of the list and a new (blank) row should be inserted, the inserted UITableView cell contains old data from the deleted cell. Worse, when multiple rows are deleted and then the user starts typing (and one blank row should be inserted), multiple deleted rows become suddenly present.</p> <p>Here is the <code>fieldChanged:</code> method that is called when one of the UITextFields in the cells is edited (where <code>self.last_cell</code> returns the last cell in the section):</p> <pre><code>- (IBAction)fieldChanged:(id)sender { // get the text field of the last row // if it has a value that is not blank, insert another row KMInputCell* last_cell = self.last_cell; if(![last_cell.textField.text isEqualToString:@""]){ [self.tableView beginUpdates]; NSIndexPath* new_cell_path = [NSIndexPath indexPathForItem:[self.tableView numberOfRowsInSection:0] inSection:0]; [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:new_cell_path] withRowAnimation:UITableViewRowAnimationAutomatic]; number_emails++; [self.tableView endUpdates]; } } </code></pre> <p>Here is the <code>commitEditingStyle:</code> method that is used to delete cells:</p> <pre><code>// Override to support editing the table view. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { // Delete the row from the data source number_emails--; [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; } else if (editingStyle == UITableViewCellEditingStyleInsert) { // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view } } </code></pre> <h2>Addition</h2> <p>Here is <code>cellForRowAtIndexPath:</code>:</p> <pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"add_email_prototype"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];= // Configure the cell... return cell; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload