【问题标题】:Data in the text field is set to null when I scroll the table view当我滚动表格视图时,文本字段中的数据设置为 null
【发布时间】:2011-05-27 06:33:52
【问题描述】:

大家好!

我在表格视图的每个单元格中都有一些文本字段,就像这样

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

以下是我的细胞构造代码

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

提前感谢

【问题讨论】:

    标签: iphone objective-c uitableview


    【解决方案1】:

    当单元格滚动出窗口时,它可能会被释放,所以你必须将输入保存在某个地方,并在单元格构造方法中从那里读取。

    以一个单元格一个文本域为例: 首先我在我的界面中声明一个 NSString,并应用到 UITextFieldDelegate 协议:

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

    然后,在 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:

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

    【讨论】:

    • 感谢您的回复,我是 iphone 新手,您能给我一些示例代码吗
    • @Ravi 我这里没有 Xcode,所以我在文本编辑器中输入了所有代码,它可能包含一些拼写错误。
    【解决方案2】:

    在用户写入时/同时保存数据(将自己设置为文本字段的代表)并在 cellForRow 检查是否保存了数据。如果是,请加载它。否则设置占位符。

    【讨论】:

      【解决方案3】:

      您必须保存在表格单元格中输入的数据,这样当您滚动表格视图时,它不会丢失。

      希望对你有帮助.....

      【讨论】:

        【解决方案4】:

        我也有同样的问题..然后我发现问题在

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

        使用

        switch(indexPath.section) 
        

        以及花括号中的整个代码......就像

        if (cell == nil) {
            //  cell = [[[CustomCellStudentData alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
            cell = [[[CustomCellStudentData alloc] initWithFrame:CGRectZero reuseIdentifier:nil] autorelease];
        
        
        switch(indexPath.section) .........
        .........
        .......
         }
        
        return cell;
        }
        

        希望这会奏效

        【讨论】:

          【解决方案5】:

          首先,因为您明确告诉它在两个地方丢失数据:

          tfText[0] = [[UITextField alloc] initWithFrame:CGRectMake(125,15, 170, 40)]; // Number one
          cell.accessoryView = tfText[0];
          tfText[0].delegate = self;
          tfText[0].text = @""; // <== Set this to something useful, instead.
          

          然后是这样的:

          cell = [[[CustomCellStudentData alloc] initWithFrame:CGRectZero reuseIdentifier:nil] autorelease];
          

          如果你想重用一个单元格,它需要那个标识符! 由于您似乎至少有两种类型的单元格,它们应该至少是两个不同的标识符...

          接下来,您将自己设置为文本字段的代表 — 这一切都很好,但您是否实现了 UITextFieldDelegate 协议并将输入的数据存储在 textFieldDidEndEditing: 中?

          有几种方法可以解决这个问题,但您可能应该为每个部分创建一个NSMutableArray 来存储输入字段中的值。 (您应该在初始化程序中进行设置。)

          哦,此刻,你正在到处泄露UITextFields...

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-05-14
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多