【问题标题】:while i am entering text in one field it is diplaying same text in some other textfield too当我在一个字段中输入文本时,它也在其他一些文本字段中显示相同的文本
【发布时间】:2012-05-18 10:17:24
【问题描述】:

当我在文本字段 1 中输入文本时,该文本也会显示在文本字段 9 中。

这是我在表格视图单元格中创建文本字段的一些代码。

我在 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;}

这里是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); 
} }

【问题讨论】:

  • 当我在一个文本字段中输入文本时,它显示在多个文本字段中.....
  • 现在我的问题很清楚了
  • 如果我重复使用单元会有什么问题
  • 能贴出完整的 cellForRowAtIndexPath 函数吗?
  • 是的,我添加了完整的 cellForRowAtIndexPath 函数,检查一下

标签: iphone uitableview uitextfield


【解决方案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;
}

【讨论】:

    【解决方案2】:

    那就是保留问题。

    您可以为每一行使用不同的 cellIdentifier。只需更改一个语句即可获得解决方案。

    用上面的方法解决这个问题。我遇到了同样的问题,我用这种方法解决了。 我希望这会对你有所帮助。

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

    【讨论】:

    • 很抱歉,这很糟糕。这不会重用任何单元格并且降低性能!请贾斯米特,除非你知道发生了什么,否则不要给出答案。即使您想避免单元重复使用,也不是这样做的方法。
    • @Rengers : 请告诉我这种类型的解决方案我能做些什么?
    • 如果您真的不想重复使用单元格,您可以删除dequeueReusableCellWithIdentifier 调用。如果您确实想重用单元格(您可能会这样做),则需要在创建单元格时设置单元格结构。然后在重用它时,您只需“重置”界面并分配单元格细节。
    • @iosDev :请删除这个接受的答案。因为我填错了所以我会删除我的答案。
    【解决方案3】:

    也许您已经为 textField 设置了一些 IBOutlet?有吗?

    【讨论】:

      猜你喜欢
      • 2021-06-14
      • 1970-01-01
      • 1970-01-01
      • 2012-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-08
      • 1970-01-01
      相关资源
      最近更新 更多