2012-05-24 57 views
0

我創建了至少10個單元格,每個單元格上都有UITextField,用於註冊頁面。當我在第一個單元格的文本字段中插入單詞時,以及當我滾動tableView時,位於另一個單元格中的文本字段顯示我剛輸入的單詞。TableView單元格中的UITextField顯示問題

以下是我的代碼。我輸入的textField數據是循環的,這是由dequeueReusableCellWithIdentifier引起的......我該如何解決這個問題?非常感謝你。

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


static NSString *CellIdentifier = @"RCell"; 
RegisterCell *cell = (RegisterCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
if (cell == nil) 
    cell = [[[RegisterCell alloc] initWithFrame:CGRectMake(0, 0, 280, 44) reuseIdentifier:CellIdentifier] autorelease]; 


if(indexPath.section == 0){ 
    NSDictionary *rowData = [self.regisLabel objectAtIndex:[indexPath row]]; 
    NSString *mainLabel = [NSString stringWithFormat:@"%@", [rowData objectForKey:@"regisLbl"]]; 
    cell.registerLabel.text = mainLabel; 

    UITextField *valTxtField = [[UITextField alloc] initWithFrame:CGRectMake(120, 5, 180, 30)]; 
    valTxtField.font = [UIFont fontWithName:@"Futura-CondensedExtraBold" size:18.0]; 
    valTxtField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; 
    valTxtField.delegate = self; 
    valTxtField.returnKeyType = UIReturnKeyDone; 
    valTxtField.autocorrectionType = UITextAutocorrectionTypeNo; 
    valTxtField.autocapitalizationType = UITextAutocapitalizationTypeNone; 

    if(indexPath.row == 0) 
    { 
     valTxtField.text = @""; 
     emailTxtFld = valTxtField; //emailTxtFld is global variable 
    } 
    if(indexPath.row == 1) 
    { 
     valTxtField.text = @""; 
     reEmailTxtFld = valTxtField; //reEmailTxtFld is global variable 
    } 

    [cell.contentView addSubview:valTxtField]; 
    [valTxtField release]; 
}  
else if(indexPath.section == 1){ 
    NSDictionary *rowData = [self.regisLabel objectAtIndex:[indexPath row]+10]; 
    NSString *mainLabel = [NSString stringWithFormat:@"%@", [rowData objectForKey:@"regisLbl"]]; 
    cell.registerLabel.text = mainLabel; 
    cell.registerTextField.enabled = NO; 
} 
else if(indexPath.section == 2){ 
    if(indexPath.row == 0){ 
     NSDictionary *rowData = [self.regisLabel objectAtIndex:[indexPath row]+11]; 
     NSString *mainLabel = [NSString stringWithFormat:@"%@", [rowData objectForKey:@"regisLbl"]]; 
     cell.registerLabel.text = mainLabel; 
     cell.registerTextField.enabled = NO; 
    } 
} 


return cell; 

}

回答

1

一個簡單的方法是隻刪除從細胞內容查看所有的子視圖添加子視圖之前,例如:

for (UIView *subview in [cell.contentView subviews]) 
    [subview removeFromSuperview]; 

更有效的辦法是做所有的創作if(cell == nil)語句中的單元格的數量,但這取決於您在表格中擁有多少個單元格。

0

權的落實移動任何視圖的創建whithin如果(細胞==零) 像這樣:

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


static NSString *CellIdentifier = @"RCell"; 
RegisterCell *cell = (RegisterCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];  

UITextField *valTxtField; 
if (cell == nil) 
{ 
    cell = [[[RegisterCell alloc] initWithFrame:CGRectMake(0, 0, 280, 44) reuseIdentifier:CellIdentifier] autorelease]; 

if(indexPath.section == 0){ 
    valTxtField = [[UITextField alloc] initWithFrame:CGRectMake(120, 5, 180, 30)]; 
    valTxtField.font = [UIFont fontWithName:@"Futura-CondensedExtraBold" size:18.0]; 
    valTxtField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; 
    valTxtField.delegate = self; 
    valTxtField.returnKeyType = UIReturnKeyDone; 
    valTxtField.autocorrectionType = UITextAutocorrectionTypeNo; 
    valTxtField.autocapitalizationType = UITextAutocapitalizationTypeNone; 
    valTxtField.tag = 100; 

    [cell.contentView addSubview:valTxtField]; 
    [valTxtField release]; 
} 
} 

valTxtField = (UITextField *)[cell.contentView viewWithTag:100]; 


if(indexPath.section == 0){ 
    NSDictionary *rowData = [self.regisLabel objectAtIndex:[indexPath row]]; 
    NSString *mainLabel = [NSString stringWithFormat:@"%@", [rowData objectForKey:@"regisLbl"]]; 
    cell.registerLabel.text = mainLabel; 

    if(indexPath.row == 0) 
    { 
     valTxtField.text = @""; 
     emailTxtFld = valTxtField; //emailTxtFld is global variable 
    } 
    if(indexPath.row == 1) 
    { 
     valTxtField.text = @""; 
     reEmailTxtFld = valTxtField; //reEmailTxtFld is global variable 
    } 
}  
else if(indexPath.section == 1){ 
    NSDictionary *rowData = [self.regisLabel objectAtIndex:[indexPath row]+10]; 
    NSString *mainLabel = [NSString stringWithFormat:@"%@", [rowData objectForKey:@"regisLbl"]]; 
    cell.registerLabel.text = mainLabel; 
    cell.registerTextField.enabled = NO; 
} 
else if(indexPath.section == 2){ 
    if(indexPath.row == 0){ 
     NSDictionary *rowData = [self.regisLabel objectAtIndex:[indexPath row]+11]; 
     NSString *mainLabel = [NSString stringWithFormat:@"%@", [rowData objectForKey:@"regisLbl"]]; 
     cell.registerLabel.text = mainLabel; 
     cell.registerTextField.enabled = NO; 
    } 
} 


return cell;