当我滚动表格视图时,文本字段中的数据设置为null [英] Data in the text field is set to null when I scroll the table view

查看:48
本文介绍了当我滚动表格视图时,文本字段中的数据设置为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

在表格视图的每个单元格中都有一些文本字段,例如

I have some text fields in each cell of the table view, like this

如果我在文本字段中输入一些数据并再次上下滚动视图,则文本字段中的数据将设置为null.为什么会发生这种情况,我该如何避免呢?

If I enter some data into the text field and scroll the view up and down again, the data in the text field is set to null. Why does this happen, how can I avoid this?

以下是我的细胞构建代码

Following is my code for cell construction

static NSString *CellIdentifier = @"Cell";

CustomCellStudentData *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    //  cell = [[[CustomCellStudentData alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    cell = [[[CustomCellStudentData alloc] initWithFrame:CGRectZero reuseIdentifier:nil] autorelease];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

switch(indexPath.section)
{
    case 0:
        switch (indexPath.row) 
        {
            case 0:
            {
                tfText[0] = [[UITextField alloc] initWithFrame:CGRectMake(125,15, 170, 40)];
                cell.accessoryView = tfText[0];
                tfText[0].delegate = self;
                tfText[0].text = @"";
                tfText[0].placeholder = @"<Student Name>";

                cell.primaryLabel.text = @"Student Name: ";
            } 
                break;
            case 1: 
            {
                tfText[1] = [[UITextField alloc] initWithFrame:CGRectMake(125,15, 170, 40)];
                cell.accessoryView = tfText[1];
                tfText[1].delegate=self;
                tfText[1].placeholder = @"<Student Age>";

                cell.primaryLabel.text = @"Student Age: ";
            } 
                break;
        }

先感谢

推荐答案

当单元格从窗口中滚动出时,它可能会被释放,因此您必须将输入内容保存在某个位置并以单元格构造方法从中读取.

when the cell is scrolled out of window, it may be released, so you must save the input in somewhere and read from there in cell construction method.

以一个带有一个文本字段的单元格为例:首先,我在接口中声明一个NSString,然后将其应用于UITextFieldDelegate协议:

Take one cell with one textfield as example: first I declare a NSString in my interface,and apply to UITextFieldDelegate protocol:

@interface someInterface : UITableViewController
<UITextFieldDelegate>
{
    NSString* contentOfTextField;
}
@property(nonatomic,retain) NSString* contentOfTextField;
@end

然后,在cellForRowAtIndex中:

then, in cellForRowAtIndex:

    static NSString *CellIdentifier = @"Cell";

    CustomCellStudentData *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
    UITextField* field = [[UITextField alloc] initWithFrame:CGRectMake(125,15, 170, 40)];
    field.delegate = self;
    field.tag = 1;

    field.text = self.contentOfTextField;
    [cell addSubView:field];
    [field release];
    return cell;

最后,每次编辑它时都应用此更新contentOfTextField:

At last, apply this to update contentOfTextField everytime you edit it:

-(void)textFieldDidEndEditing:(UITextField*)textField
{
    UITableViewCell* cell = [self.tableView cellForRowAtIndexPath(/*the indexPath of your cell*/)];
    UITextField* t = (UITextField*)[cell viewWithTag:1];
    self.contentOfTextField = t.text;
}

这篇关于当我滚动表格视图时,文本字段中的数据设置为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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