我正在尝试创建类似于iPhone上照片库中的YouTube视频上传器视图的表格视图。
这是基本设置。
我创建了一个包含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];
}
答案 0 :(得分:0)
我能想到的唯一的事情是,当你插入一行时,整个表格视图会被重新加载,如果你没有将你的单元格正确地添加到单元格队列中,它们就不会回到它们所处的状态,因此你看到插入的空单元格,只是一个猜测,希望它有所帮助。
答案 1 :(得分:0)
快速更新:
无所谓,但我注意到你不需要:
[self.tableView beginUpdates]
[self.tableView endUpdates]
因为您正在执行一项操作。不确定是否重要(不应该)。
更新
我应该说这些问题很常见。这是与您的问题相关的帖子
http://www.bdunagan.com/2008/12/08/uitextview-in-a-uitableview-on-the-iphone/
此外,其他人已经抽出了这个。具体来说,我尝试了这个没有这样的问题:
http://furbo.org/2009/04/30/matt-gallagher-deserves-a-medal/
您可以使用: http://github.com/joehewitt/three20/
但它有一点学习曲线。
解决此问题的另一个Stackoverflow问题:
Editing a UITextField inside a UITableViewCell fails
请原谅我没有直接回答你的问题,但我认为解决方案可能包含在其中一个链接中。
原始答案:
尝试:
[newTextField setNeedsDisplay];
有时,通过更新UITextView / UITextField内容,tableviews可能会“粘滞”。
如果这不起作用,请确保您的支持模型也已正确更新。您没有显示任何代码表明您更新了模型(虽然我假设您这样做了,否则它可能会抛出异常)。