felixwcf
felixwcf

Reputation: 2107

UITextField display issue in TableView cell

I created at least 10 cells with each UITextField on it, for registration page. When I insert the words on the textfield in the first cell, and when I scrolled the tableView, the textfield which is in another cell show up the word that I just typed.

Below is my code. The textField data which I input is looping, which is caused by dequeueReusableCellWithIdentifier... How can I solve this problem? Thank you very much.

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

}

Upvotes: 0

Views: 334

Answers (2)

Omar Abdelhafith
Omar Abdelhafith

Reputation: 21221

The right implementation is to move the creation of any view whithin if (cell == nil) Like so:

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

Upvotes: 0

JDx
JDx

Reputation: 2655

An easy way is to just remove all subviews from the cells contentView before you add subviews, example:

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

A more efficient way would be to do all the creation of cells inside the if (cell == nil) statement, but that depends on how many cells you have in the table.

Upvotes: 1

Related Questions