当我在文本字段 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);
} }