我在桌面视图中添加了几个单元格,每个单元格都有一个右边的textField让用户输入文本。我发现,当我向下滚动并返回时,前几行的输入将消失。有人知道有什么问题吗?UITableViewCell中的文本向下滚动时丢失
下面是一张我的代码:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
//init cell
static NSString *TableIdentifier = @"MyIdentifier";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:TableIdentifier];
if(cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero
reuseIdentifier:TableIdentifier] autorelease];
}
//set cell for each row
if([indexPath row] == 0)
{
cell.textLabel.text = @"Row A";
txt_A = [self CreateTextField]; //create textField
[cell.contentView addSubview:txt_A]; //add textField to cell
}
else if([indexPath row] == 1)
{
cell.textLabel.text = @"Row B";
txt_B = [self CreateTextField];
[cell.contentView addSubview:txt_B];
}
else{...} }
我该如何避免让用户重新创建文本字段? – 2009-09-16 10:39:10
当在文本字段中插入输入时(或单元格消失时),可以将用户输入存储在数组中,并将行号作为索引。当单元格被创建时(在你当前的代码中),你可以检查是否有一个数组条目的行号作为索引。如果存在,则使用存储的文本初始化文本字段,如果没有初始化空字段。 – Jake 2009-09-16 11:32:08
感谢MiRAGe ...这帮了很多! – iwasrobbed 2010-05-08 22:09:53