我在桌面視圖中添加了幾個單元格,每個單元格都有一個右邊的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