Iphone uitableviewcell的数据在滚动时相互重叠

Iphone uitableviewcell的数据在滚动时相互重叠,iphone,uitableview,Iphone,Uitableview,我有一个包含四个部分的tableview,所有部分在不同的行中都有两个文本字段和一个标签。我添加了一些文本作为textfield的占位符。最初,数据显示良好,但当我滚动tableview时,单元格开始有重叠数据。 我的代码: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier

我有一个包含四个部分的tableview,所有部分在不同的行中都有两个文本字段和一个标签。我添加了一些文本作为textfield的占位符。最初,数据显示良好,但当我滚动tableview时,单元格开始有重叠数据。
我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if(indexPath.row==0) {
    UITextField *txtName = [[UITextField alloc] initWithFrame:CGRectMake(5, 5, 300, 30)];
    txtName.placeholder = @"Full Name";
    [cell.contentView addSubview:txtName];
    [txtName release];
}
else if(indexPath.row==1) {
    UITextField *txtEmail = [[UITextField alloc] initWithFrame:CGRectMake(5, 5, 300, 30)];
    txtEmail.placeholder = @"Email";
    [cell.contentView addSubview:txtEmail];
    [txtEmail release];
}
else if(indexPath.row==2){
    cell.textLabel.text = @"Select Date of Birth";
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
// Configure the cell...

return cell;
}

提前感谢

Pankaj

您只需要在初始化单元格的代码块中创建文本字段。请记住,表格视图会回收单元格,因此当您从屏幕上滚动时,您会得到一个已经有文本字段的重复使用和回收的单元格。然后,您将创建一个新的textfield,并将新的textfield覆盖在现有的textfield上,从而获得重叠

这是经过适当重构的代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

        //create the textField here, and we will reuse it and reset its data for each row.
        UITextField *txtField = [[UITextField alloc] initWithFrame:CGRectMake(5, 5, 300, 30)];
        [cell.contentView addSubview:txtField];
        txtField.tag=110; //should declare a constant that uniquely defines your textField;
        [txtField release];

    }

    // Configure the cell...

    //ok, now we retrieve the textField and set its data according to the row.
    UITextField *txtField = (UITextField *)[cell.contentView viewWithTag:110];

    if(indexPath.row==0) {
        txtField.placeholder = @"Full Name";   
    }
    else if(indexPath.row==1) {
        txtField.placeholder = @"Email";    
    }
    else if(indexPath.row==2){
        txtField.placeholder = nil;  //? did you mean to set something here?
        cell.textLabel.text = @"Select Date of Birth";
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }


    return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";
UITextField *txtField;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

//init the textField here, and we will reuse it and reset its data for each row.
txtField = [[UITextField alloc] initWithFrame:CGRectMake(5, 5, 300, 30)];
[cell.contentView addSubview:txtField];
txtField.tag=110; //should declare a constant that uniquely defines your textField;
[txtField release];

}
else{

// if the textfield is alread created and now dequed

    //ok, now we retrieve the textField and set its data according to the row.
    txtField = (UITextField *)[cell.contentView viewWithTag:110];
}    


    if(indexPath.row==0) {
        txtField.placeholder = @"Full Name";   
    }
    else if(indexPath.row==1) {
        txtField.placeholder = @"Email";    
    }
    else if(indexPath.row==2){
        txtField.placeholder = nil;  //? did you mean to set something here?
        cell.textLabel.text = @"Select Date of Birth";
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }


    return cell;
    }

我刚刚修改了前一个,因为它缺少一个else条件,导致了一个错误的访问错误。修改后的代码如下:


您需要在初始化单元格的代码块和初始化之前创建文本字段,并将此文本字段添加到初始化单元格的代码块中。请记住,表格视图会回收单元格,因此当您从屏幕上滚动时,您会得到一个已经有文本字段的重复使用和回收的单元格。然后,您将创建一个新的textfield,并将新的textfield覆盖在现有的textfield上,从而获得重叠

这是经过适当重构的代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

        //create the textField here, and we will reuse it and reset its data for each row.
        UITextField *txtField = [[UITextField alloc] initWithFrame:CGRectMake(5, 5, 300, 30)];
        [cell.contentView addSubview:txtField];
        txtField.tag=110; //should declare a constant that uniquely defines your textField;
        [txtField release];

    }

    // Configure the cell...

    //ok, now we retrieve the textField and set its data according to the row.
    UITextField *txtField = (UITextField *)[cell.contentView viewWithTag:110];

    if(indexPath.row==0) {
        txtField.placeholder = @"Full Name";   
    }
    else if(indexPath.row==1) {
        txtField.placeholder = @"Email";    
    }
    else if(indexPath.row==2){
        txtField.placeholder = nil;  //? did you mean to set something here?
        cell.textLabel.text = @"Select Date of Birth";
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }


    return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";
UITextField *txtField;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

//init the textField here, and we will reuse it and reset its data for each row.
txtField = [[UITextField alloc] initWithFrame:CGRectMake(5, 5, 300, 30)];
[cell.contentView addSubview:txtField];
txtField.tag=110; //should declare a constant that uniquely defines your textField;
[txtField release];

}
else{

// if the textfield is alread created and now dequed

    //ok, now we retrieve the textField and set its data according to the row.
    txtField = (UITextField *)[cell.contentView viewWithTag:110];
}    


    if(indexPath.row==0) {
        txtField.placeholder = @"Full Name";   
    }
    else if(indexPath.row==1) {
        txtField.placeholder = @"Email";    
    }
    else if(indexPath.row==2){
        txtField.placeholder = nil;  //? did you mean to set something here?
        cell.textLabel.text = @"Select Date of Birth";
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }


    return cell;
    }

+1但如果在一行中设置accessoryType,则应在其他行中禁用它。嗨,Jason,谢谢回复,但当我滚动几次时,textlabel的文本也会出现在第0行中。你想让它隐藏在第0行中,并在其他行中可见吗?textfield将出现在第0行和第1行,还有一件事,当我按下submit按钮时,我如何唯一地标识所有行的textfield?确定。然后在文本字段上有一个句柄后,调用此代码。txtField.hidden=indexPath.row==0;