2012-05-18 26 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; 
} 
相關問題