2012-05-18 28 views
0

当我在文本框输入文本1的文本也显示在文本框9而我在一个字段中输入文本它在其他一些文本框diplaying相同的文字太

这里是一些代码,我怎么糟蹋在tableview单元格中创建文本字段。

我cellforRowatintexpath方法

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

加入的tableview细胞文本框。像的下方,这里是法getTextFieldValue方法

- (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); 
} } 
+0

,问题是... :) –

+0

而我在它显示在多个文本框一个文本框输入文字..... – iosDev

+0

现在我的问题是清楚的,你 – iosDev

回答

0

这是保留的问题是存在的。

您可以为每一行取不同的cellIdentifier。只有改变你的解决方案的一个陈述。

用上面的方法解决这个问题。我面临同样的问题&我解决这个方法。 我希望这会帮助你。

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

我相信它会为你工作。 – Jasmit

+0

给出错误初始化元素是不恒定的........ – iosDev

+0

现在我应该怎么做 – iosDev

0

也许你已经设置了一些IBOutlet中的文本字段?你是否?

1

首先,请重写getTextFieldValue方法是这样的:

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

您不断添加UITextFields到小区,即使它的重复使用。尝试将该代码放入if语句中。

尽管如此,您应该将文本字段的文本设置为零。否则它仍然会出现在出列的单元格上(滚动时)。

事情是这样的:

- (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; 
} 
相关问题