2012-05-24 2 views
0

Я создал по меньшей мере 10 ячеек с каждым UITextField, на странице регистрации. Когда я вставляю слова в текстовое поле в первой ячейке, и когда я прокручиваю tableView, текстовое поле, находящееся в другой ячейке, отображает слово, которое я только что напечатал.Проблема с отображением UITextField в ячейке TableView

Ниже приведен мой код. Данные 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

Простой способ просто удалить все подвидов из клеток contentView, прежде чем добавить подвидов, например:

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; 
Смежные вопросы