iPhoneのフォトギャラリーのYouTubeビデオアップローダービューに似たテーブルビューを作成しようとしています。UITableViewCellのUITextField - 新しいセルを追加する
これは基本的な設定です。
UITextFieldを含むカスタムUITableViewCellを作成しました。私のテーブルにセルを表示することはすばらしく、問題なくテキストを編集できます。イベントフックを作成して、テキストフィールドでテキストが変更されたときに表示することができます。
[textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged]
私がしたいことはこれです。ユーザーが最初に私は、現在のセルの下のテーブルビューに新しいセルを挿入するテキストを編集するとき(newIndexPathは、適切な位置に先立って計算されます):私は、セルの挿入コード実行時に
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationBottom];
[self.tableView endUpdates];
問題がありますセルが作成されますが、テキストフィールドのテキストは短時間更新されますが、キーボードは閉じられ、テキストフィールドは空の文字列に戻されます。
何か助けが素晴らしいだろう!私は一日中このことについて頭を叩いています。私はここにあると考えることができ
- (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
いいえ、挿入されていないテキストフィールドは、入力されたテキストを維持します。それは、テキストが消えるセルが挿入されているときだけです。 –
上記のtagCellIdentifierは、Interface Builderのセルのために設定した識別子である "SimpleTextFieldTableCell"に設定されています。 –