2011-04-30 83 views
1

我有一个tableview有四个部分,所有部分有两个文本框和一个标签在不同的行中。我添加了一些文本作为文本框的占位符。最初数据显示正常,但当我滚动tableview时,单元格开始有重叠的数据。
我的代码:uitableviewcell的数据在滚动上相互重叠

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

}

在此先感谢
潘卡

回答

16

你只需要在代码inits细胞块来创建文本字段。请记住,表格视图可以回收单元格,因此当您从屏幕上滚动时,您将得到一个已经有文本字段的重用单元格。然后,您将创建一个新的文本字段并将新的文本字段覆盖在现有的文本字段上,因此您会重叠。

这里是你的代码重构正确

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

+1,但如果你在一个行设置accessoryType你应该在其他行禁用它。 – 2011-04-30 17:10:29

+0

嗨贾森,谢谢你的回复,但textlabel文本出现在第0行也滚动几次。 – pankaj 2011-04-30 17:58:39

+0

你想让它隐藏在第0行,并在其他地方可见吗? – 2011-04-30 18:02:16

2

我刚刚修改了前一个失踪发出一个坏的访问错误的其他条件。修改后的代码如下:

您需要在嵌入单元格和init的代码块之前创建文本字段,并将该文本字段添加到位于单元格中的代码块中。请记住,表格视图可以回收单元格,因此当您从屏幕上滚动时,您将得到一个已经有文本字段的重用单元格。然后,您将创建一个新的文本字段并将新的文本字段覆盖在现有的文本字段上,因此您会重叠。

这里是你的代码重构正确

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