简体   繁体   中英

UITextField text replaces with same text while scrolling in uitableviewcell

在此处输入图片说明检查这张图片在此处输入图片说明 I placed three textfields in one row and 1 textfield in another row. I took all the textfields in one array and wrote the following in 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;
}

and in the textFieldDidEndEditing method wrote as follows.

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

My Problem is once i enter text in one textfield and scroll up and down then that same text is taking to remaining two textfields in that same row.

and if i enter text in three textfields and then scroll , that is taking text from the last textfield for all.

Sounds like you are having an issue with cell reuse.

To increase performance, UITableView will reuse a cell off screen instead of creating a new cell. When a cell gets reused, it does not clear it out. Its your job to reassign what information you want in it.

you set the information something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM