uitableviewcell的数据在滚动时相互重叠 [英] data of uitableviewcell overlapping with each other on scrolling

查看:129
本文介绍了uitableviewcell的数据在滚动时相互重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我的代码:

I have a tableview with four sections and all of the sections have two textfields and a label in different rows. I have added some text as placeholder of textfield. Initially the data appears fine but when I scroll the tableview the cell starts to have overlapped data.
My Code:

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

thanks in advance
Pankaj

推荐答案

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

You need to create your text fields only in the block of code that inits the cell. Remember that the table view recycles cells so as you scroll off the screen you get a reused and recycled cell that already has a textfield. You are then creating a new textfield and overlaying the new textfield on the existing one, hence you get overlapping.

这里是您的代码已正确重构

here is your code properly refactored

- (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的数据在滚动时相互重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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