向下滚动时UITableViewCell中的文本丢失 [英] Text in UITableViewCell missing when scroll down

查看:46
本文介绍了向下滚动时UITableViewCell中的文本丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表视图中添加了几个单元格,每个单元格的右侧都有一个textField,允许用户输入文本.我发现当我向下滚动并返回时,前几行的输入将消失.有人知道出什么问题吗?

I added several cells to a tableview and each cell has a textField in the right to let users input texts. I find that when I scroll down and go back, the input of the first few lines will disappear. Does anybody know what's the problem?

以下是我的一部分代码:

The following is a piece of my codes:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
    *)indexPath
        {
            //init cell
            static NSString *TableIdentifier = @"MyIdentifier";
            UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:TableIdentifier];
            if(cell == nil) {
                cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero 
                                        reuseIdentifier:TableIdentifier] autorelease];
            }

            //set cell for each row
            if([indexPath row] == 0)
            {
                cell.textLabel.text = @"Row A";
                txt_A = [self CreateTextField];         //create textField
                [cell.contentView addSubview:txt_A];        //add textField to cell
            }
            else if([indexPath row] == 1)
            {
                cell.textLabel.text = @"Row B";
                    txt_B = [self CreateTextField];
                    [cell.contentView addSubview:txt_B];
            }
            else{...} }

推荐答案

您的意思是用户输入将消失?如果是这样,这可能是因为cellForRowAtIndexPath:重用了单元格并将再次执行[self CreateTextField].

You mean the user input will disappear? If so this is probably because cellForRowAtIndexPath: reuses the cell and will execute the [self CreateTextField] again.

因此,除非您在单元消失之前存储用户输入,否则您的单元将被重新绘制为空并重新初始化为空.

So, unless you store the user input before the cell disappears, your cells will be redrawn empty and reinitialized empty.

如果您确实存储了用户输入,则可以使用正确的用户输入重新初始化单元格.

If you do store the user input, you could reinitialize the cell with the right user input.

这篇关于向下滚动时UITableViewCell中的文本丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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