iosDev
iosDev

Reputation: 604

while i am entering text in one field it is diplaying same text in some other textfield too

When I am entering text in textfield 1 that text is also displayed in textfield 9.

here is some code what i did to create textfield in tableview cell.

i added textfield in tableview cell .like below in cellforRowatintexpath method

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

static NSString *CellIdentifier = @"myCell";
//UILabel* nameLabel = nil;
cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
     cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
else
{
    UILabel *titleLbl = (UILabel *)[cell viewWithTag:1];
    [titleLbl removeFromSuperview];
}
nameLabel = [[UILabel alloc] initWithFrame:CGRectMake( 7.0, 10.0, 160.0, 44.0 )];
nameLabel.text = [labels objectAtIndex:indexPath.row];
nameLabel.font = [UIFont boldSystemFontOfSize:12]; 
nameLabel.lineBreakMode = UILineBreakModeWordWrap;
nameLabel.tag =1;
nameLabel.numberOfLines =0;
[nameLabel sizeToFit];
CGSize expectedlabelsize = [nameLabel.text sizeWithFont:nameLabel.font constrainedToSize:nameLabel.frame.size lineBreakMode:UILineBreakModeWordWrap];
CGRect newFrame =nameLabel.frame;
newFrame.size.height =expectedlabelsize.height;
[cell.contentView addSubview: nameLabel];
[nameLabel release]; 


//adding textfield to tableview cell. 
tv = [[UITextField alloc] initWithFrame:CGRectMake(nameLabel.frame.origin.x+nameLabel.frame.size.width+2, 10.0, cell.contentView.bounds.size.width, 30)];
tv.tag = indexPath.row + 2;
//tv.text =[labels objectAtIndex:indexPath.row];
tv.font = [UIFont boldSystemFontOfSize:12];


//this method will take text from textfield of tableview cell using their individual tag
[tv addTarget:self action:@selector(getTextFieldValue:) forControlEvents:UIControlEventEditingDidEnd];

[cell.contentView addSubview:tv];

[tv setDelegate:self];


[tv release];   


return cell;}

and here is the method getTextFieldValue method

- (void) getTextFieldValue:(UITextField *)textField{ 

if (textField.tag == 2) { 

    //NSString *value1 = [NSString stringWithFormat:@"%@",textField.text];
    //NSLog(@"value1 is %@",value1);.

    NSLog(@"2--->%@",textField.text); 
}else if(textField.tag == 3){ 
    NSLog(@"3--->%@",textField.text); 
}else if(textField.tag == 4){ 
    NSLog(@"4--->%@",textField.text); 
    } 
else if(textField.tag == 5){ 
    NSLog(@"5--->%@",textField.text); 
} 
else if(textField.tag == 6){ 
    NSLog(@"6--->%@",textField.text); 
} 
else if(textField.tag == 7){ 
    NSLog(@"7--->%@",textField.text); 
} 
else if(textField.tag == 8){ 
    NSLog(@"8--->%@",textField.text); 
} 
else if(textField.tag == 9){ 
    NSLog(@"9--->%@",textField.text); 
} 
else if(textField.tag == 10){ 
    NSLog(@"10--->%@",textField.text); 
} 
else if(textField.tag == 11){ 
        NSLog(@"11--->%@",textField.text); 
} 
else if(textField.tag == 12){ 
        NSLog(@"12--->%@",textField.text); 
} }

Upvotes: 0

Views: 96

Answers (3)

Jasmit
Jasmit

Reputation: 241

That is the retain problem is there.

You can take different cellIdentifier for each row. Only change one statement you get your solution.

Solve this problem with above method. i was facing same problem & i solve with this method. I hope this will help you.

NSString *CellIdentifier = [NSString stringWithFormat:@"cellidentifier-%d",indexPath.row];

Upvotes: 0

Rengers
Rengers

Reputation: 15238

First of all, please rewrite your getTextFieldValue method like this:

- (void)getTextFieldValue:(UITextField *)textField { 
    NSLog(@"%d--->%@", textField.tag, textField.text); 
}

You keep adding UITextFields to the cell, even when it's reused. Try putting that code inside the if statement.

You should set the text of the text field to nil every time though. Else it will still be there on dequeued cells (when scrolling).

Something like this:

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

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

if(cell == nil)
{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake( 7.0, 10.0, 160.0, 44.0 )];
    //TODO: Set up nameLabel here
    [cell.contentView addSubview: nameLabel];
    [nameLabel release];

    UITextField *tv = [[UITextField alloc] init];
    tv.font = [UIFont boldSystemFontOfSize:12];
    [tv setDelegate:self];
    [tv addTarget:self action:@selector(getTextFieldValue:) forControlEvents:UIControlEventEditingDidEnd];
    [cell.contentView addSubview:tv];
    [tv release];
}

UILabel *nameLabel = (UILabel *)[cell viewWithTag:1];
nameLabel.text = [labels objectAtIndex:indexPath.row];
[nameLabel sizeToFit];

return cell;
}

Upvotes: 1

lu yuan
lu yuan

Reputation: 7227

Maybe you have set some IBOutlet to the textField? Did you?

Upvotes: 0

Related Questions