UITextField在UITableViewCell中 - 添加新单元格 [英] UITextField in UITableViewCell - adding new cells

查看:72
本文介绍了UITextField在UITableViewCell中 - 添加新单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建类似于iPhone上照片库中的YouTube视频上传器视图的表格视图。

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

这是基本设置。

我创建了一个包含UITextField的自定义UITableViewCell。在我的表格中显示单元格效果很好,我可以毫无问题地编辑文本。我创建了一个事件挂钩,以便我可以查看文本字段中文本的更改时间。

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]

我想做的就是这个。当用户第一次编辑文本时,我想在当前单元格下方的表视图中插入一个新单元格(newIndexPath在正确位置之前计算):

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];
}


推荐答案

我唯一想到的就是也就是说,当你插入一行时,整个表视图被重新加载,如果你没有将你的单元格正确地添加到单元格队列中,它们将不会回到它们所处的状态,因此你看到的是由于插入,只是一个猜测,希望它有所帮助。

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.

这篇关于UITextField在UITableViewCell中 - 添加新单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆