UITextField in UITableViewCell - adding new cells

11,006

Only thing i can think of here is that perhaps when you insert a row the whole table view is reloaded, if you did not add your cell properly to the cell queue, they wont come back in the state that they were , therefore you are seeing empty cells as a result of the insert, just a guess, hope it helps.

Share:
11,006
Colin Humber
Author by

Colin Humber

Updated on June 04, 2022

Comments

  • Colin Humber
    Colin Humber almost 2 years

    I am trying to create a table view similar to the YouTube video uploader view in the Photo Gallery on the iPhone.

    Here's the basic setup.

    I have a custom UITableViewCell created that contains a UITextField. Displaying the cell in my table works great and I can edit the text with no problems. I created an event hook so I can view when the text has changed in the text field.

    [textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged]
    

    What I want to do is this. When the user first edits the text I want to insert a new cell into the table view below the current cell (newIndexPath is calculated prior to the proper position):

    [self.tableView beginUpdates];
    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationBottom];
    [self.tableView endUpdates];
    

    Problem is when I run the cell insert code the cell is created but the text field's text updated briefly, but then the keyboard is dismissed and the text field is set back to an empty string.

    Any help would be awesome! I've been banging my head about this one all day.

    - (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
    {
        if (section == 0)
            return 2;
        else
            return self.tags.count;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        ...
        cell = (SimpleTextFieldTableCell *)[tableView dequeueReusableCellWithIdentifier:tagCellIdentifier];
    
        if (cell == nil)
        {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"SimpleTextFieldTableCell" owner:nil options:nil] lastObject];
        }
    
        ((SimpleTextFieldTableCell *)cell).textField.delegate = self;
        ((SimpleTextFieldTableCell *)cell).textField.tag = indexPath.row;
        ((SimpleTextFieldTableCell *)cell).textField.text = [self.tags objectAtIndex:indexPath.row];
        [((SimpleTextFieldTableCell *)cell).textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    - (void)textFieldDidChange:(id)sender
    {
        UITextField *textField = sender;
    
        [self.tags replaceObjectAtIndex:textField.tag withObject:textField.text];
        if (textField.text.length == 1)
        {
            [textField setNeedsDisplay];
            [self addTagsCell];
        }
    }
    
    - (void)addTagsCell
    {
        NSString *newTag = @"";
        [self.tags addObject:newTag];
    
        NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:self.tags.count - 1 inSection:1];
        [self.tableView beginUpdates];
        [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationBottom];
        [self.tableView endUpdates];
    }
    
    • TahoeWolverine
      TahoeWolverine almost 15 years
      Does the textfield blank if you comment out the insert?
    • Colin Humber
      Colin Humber almost 15 years
      No, the textfield maintains the entered text if there's no insert. It's only when a cell is being inserted that the text disappears.
    • Colin Humber
      Colin Humber almost 15 years
      tagCellIdentifier above is set to "SimpleTextFieldTableCell" which is the identifier I setup for the cell in Interface Builder.
  • Colin Humber
    Colin Humber almost 15 years
    I'm using a cell created with interface builder so it doesn't run through the initWithStyle:reuseIdentifier. Could that be a problem?
  • Colin Humber
    Colin Humber almost 15 years
    I tried calling setNeedsDisplay to no avail. I adding the method implementations to my original post, including model updates.
  • Colin Humber
    Colin Humber almost 15 years
    I just double checked and the identifier I'm using to dequeue the cell is the same one I assigned to the cell in IB.
  • Colin Humber
    Colin Humber almost 15 years
    There are 3 different types of cells I'm using and each has its own identifier.
  • Colin Humber
    Colin Humber almost 15 years
    Ah, I think you're right about the beginUpdates and endUpdates calls. I thought the animation only occurred if the insert rows method was called within there, but it looks like that's just for groups of inserts and deletes. It's odd, since the UITextField in the UITableViewCell works fine by itself. No issues at all. It's just the inserting of a cell within a UITextField changed event that's causing the issue. Funny you should mention Three20. I'm actually using it for the majority of this app. There were some places that manually creating the table made more sense, this being one of them.
  • Daniel
    Daniel almost 15 years
    what do you mean by types? If you have 20 cells you should have 20 different identifiers
  • Daniel
    Daniel almost 15 years
    the above comment is assuming that each cell has its own state (meaing each cell contents can potentially be diffrent) if you are having users type in each cell then each cell can have its own state
  • Colin Humber
    Colin Humber almost 15 years
    My table has 2 sections, 3 rows total. The first two rows are populated by an external table and a UIPickerView control. It's just the 3rd cell that is populated by direct user input via the text field.
  • Colin Humber
    Colin Humber almost 15 years
    I see what you're saying with the cell state. I have a workaround which works great but doesn't explain why things weren't working before. I now have a row at the bottom of the section in question that, when clicked, adds a new Tag row. This works great with no state issues. It seems like the problem was running the insertRowsAtIndexPaths: method during a text change event on the UITextField.