【问题标题】:UITextField text replaces with same text while scrolling in uitableviewcellUITextField 文本在 uitableviewcell 中滚动时替换为相同的文本
【发布时间】:2012-08-28 16:17:17
【问题描述】:

我在一行中放置了三个文本字段,在另一行中放置了一个文本字段。我将所有文本字段放在一个数组中,并在cellForRowAtIndexPath

中写入以下内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"MyIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier ];
        if (indexPath.row == 0) {

            UITextField *tf = [[UITextField alloc]initWithFrame:CGRectMake(10, 10, 100, 30)];
            tf.delegate = self;
            tf.tag = 16;
            tf.borderStyle = UITextBorderStyleRoundedRect;
            [cell.contentView addSubview:tf];
            [tf release];

            UITextField *tf1 = [[UITextField alloc]initWithFrame:CGRectMake(150, 10, 100, 30)];
            tf1.delegate = self;
            tf1.tag = 17;
            tf1.borderStyle = UITextBorderStyleRoundedRect;
            [cell.contentView addSubview:tf1];
            [tf1 release];
        }
        else {
            UITextField *tf3 = [[UITextField alloc]initWithFrame:CGRectMake(10, 10, 100, 30)];
            tf3.delegate = self;
            tf3.tag = 18;
            tf3.borderStyle = UITextBorderStyleRoundedRect;
            [cell.contentView addSubview:tf3];
            [tf3 release];

        }

    }
    UITextField *tf = (UITextField *)[cell.contentView viewWithTag:16];
    tf.text =  [m_textFieldArray objectAtIndex:indexPath.row];

    UITextField *tf1 = (UITextField *)[cell.contentView viewWithTag:17];
    tf1.text =  [m_textFieldArray objectAtIndex:indexPath.row];
    UITextField *tf3 = (UITextField *)[cell.contentView viewWithTag:18];
    tf3.text =  [m_textFieldArray objectAtIndex:indexPath.row];
   // cell.textLabel.text = @"Hiiiii";
    return cell;
}

并在textFieldDidEndEditing方法中写如下。

[textFieldsDataArray replaceObjectAtIndex:currentIndexPath.row withObject:textField.text];

我的问题是,一旦我在一个文本字段中输入文本并上下滚动,然后相同的文本将占用同一行中剩余的两个文本字段。

如果我在三个文本字段中输入文本然后滚动,那就是从最后一个文本字段中获取所有文本。

【问题讨论】:

    标签: objective-c uitableview uiscrollview uitextfield


    【解决方案1】:

    听起来您在单元重用方面遇到了问题。

    为了提高性能,UITableView 将在屏幕外重用一个单元格,而不是创建一个新单元格。当一个单元格被重用时,它不会将其清除。重新分配你想要的信息是你的工作。

    您将信息设置为:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]
    
        // If the cell is nil, it is a new cell and you need to init it    
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
    
        // setting the cell's information
        cell.textLabel.text = [dataSource objectAtIndex:indexPath.row];
        cell.textLabel.textAlignment = UITextAlignmentLeft;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    
        return cell;
    }
    

    【讨论】:

    • 我在编辑时写了现在检查一次...一旦在第一个文本字段中输入文本,如果我向上和向下滚动相同的文本也将带到同一行中的第二个文本字段... .
    • 你能把你所有的代码都放在代码块里吗?这样读起来很痛苦
    • 你能弄明白吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    相关资源
    最近更新 更多