0

テキストフィールド1にテキストを入力すると、そのテキストはテキストフィールド9にも表示されます。

これが、テーブルビューセルにテキストフィールドを作成するために行ったコードです。

以下の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); 
} }
4

3 に答える 3

1

まず、getTextFieldValueメソッドを次のように書き直してください。

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

再利用されている場合でも、セルにUITextFieldsを追加し続けます。そのコードをifステートメント内に配置してみてください。

ただし、テキストフィールドのテキストは毎回nilに設定する必要があります。それ以外の場合は、デキューされたセルに引き続き存在します(スクロール時)。

このようなもの:

- (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;
}
于 2012-05-18T10:48:44.903 に答える
0

それは保持の問題があります。

行ごとに異なるcellIdentifierを使用できます。ソリューションを取得するステートメントを1つだけ変更してください。

上記の方法でこの問題を解決します。私は同じ問題に直面していました&私はこの方法で解決します。これがお役に立てば幸いです。

NSString *CellIdentifier = [NSString stringWithFormat:@"cellidentifier-%d",indexPath.row];
于 2012-05-18T11:23:44.690 に答える
0

たぶん、あなたはいくつかのIBOutletをtextFieldに設定しましたか?しましたか?

于 2012-05-18T10:30:27.017 に答える