J'essaie de créer une vue de table similaire à la vue de téléchargement de vidéos YouTube dans la galerie de photos sur l'iPhone.UITextField dans UITableViewCell - ajout de nouvelles cellules
Voici la configuration de base.
J'ai créé un UITableViewCell personnalisé qui contient un UITextField. L'affichage de la cellule dans ma table fonctionne très bien et je peux éditer le texte sans problèmes. J'ai créé un crochet d'événement afin que je puisse voir quand le texte a changé dans le champ de texte.
[textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged]
Ce que je veux faire est ceci. Lorsque l'utilisateur d'abord édite le texte que je veux insérer une nouvelle cellule dans la vue tableau ci-dessous la cellule courante (newIndexPath est calculé avant la bonne position):
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationBottom];
[self.tableView endUpdates];
Le problème est quand je lance le code d'insertion de cellules de la La cellule est créée mais le texte du champ de texte est mis à jour brièvement, mais le clavier est fermé et le champ de texte est remis à une chaîne vide.
Toute aide serait géniale! Je me suis cogné la tête à propos de celui-ci toute la journée.
- (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];
}
Est-ce que le champ de texte est vide si vous commentez l'insertion? – TahoeWolverine
Non, le champ de texte conserve le texte saisi s'il n'y a pas d'insertion. C'est seulement quand une cellule est insérée que le texte disparaît. –
tagCellIdentifier ci-dessus est défini sur "SimpleTextFieldTableCell" qui est l'identifiant que j'ai configuré pour la cellule dans Interface Builder. –