我有三个单元格,其中我有每个文本字段。现在我希望用户点击哪个文本框。此方法textFieldDidEndEditing为我提供了用户输入的值,但我没有得到任何文本字段的标记。
这是我的代码:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{ switch (section)
{ case 0: return @"";
case 1: return @"";
default:return @"";
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if(optconscodecount != 0 && gotOK == 2)
return 2;
else
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
switch(section)
{ case 0: try=1; return conscodecount;
case 1: try=2; return optconscodecount;
default:return 0;}
}
// Heights per row
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{ int section = [indexPath section];
//int row = [indexPath row];
switch (section)
{case 0:return 80.0f;
case 1:return 80.0f;
default:return 80.0f;}
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"\n%@", appDelegate.conscodeNameArray);
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
row = [indexPath row];
section = [indexPath section];
switch (section)
{case 0:try=1;cell = [tableView dequeueReusableCellWithIdentifier:@"textCell"];
if (!cell)
{cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"textCell"] autorelease];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20.0f, 10.0f, 280.0f, 20.0f)];
[label setText:[appDelegate.conscodeNameArray objectAtIndex:row]];[cell addSubview:label]; [label release];
[cell addSubview:[[UITextField alloc] initWithFrame:CGRectMake(20.0f, 40.0f, 280.0f, 30.0f)]];
}
UITextField *tf = [[cell subviews] lastObject];
tf.placeholder = [appDelegate.conscodeNameArray objectAtIndex:row];
tf.tag =tagcount;
tagcount=tagcount+1;
tf.delegate = self;
tf.borderStyle = UITextBorderStyleRoundedRect;
return cell;
break;
case 1:
try=2;
cell = [tableView dequeueReusableCellWithIdentifier:@"textCell"];
if (!cell)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"textCell"] autorelease];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20.0f, 10.0f, 280.0f, 20.0f)];
[label setText:[appDelegate.optconscodeNameArray objectAtIndex:row]];
[cell addSubview:label];
[label release];
[cell addSubview:[[UITextField alloc] initWithFrame:CGRectMake(20.0f, 40.0f, 280.0f, 30.0f)]];
}
UITextField *my = [[cell subviews] lastObject];
my.tag = 0;
my.tag = my.tag+1;
my.placeholder = [appDelegate.optconscodeNameArray objectAtIndex:row];
my.delegate = self;
my.borderStyle = UITextBorderStyleRoundedRect;
return cell;
break;
return cell;
break;
default:
break;
}
return cell;
}
答案 0 :(得分:1)
nameField.tag = 1;
agefield.tag = 2;
//在委托方法中检查
if (textField.tag == 1) {
NSLog(@" clicked in Name field");
} else if (textField.tag ==2) {
NSLog(@" clicked in Age field");
}
答案 1 :(得分:0)
如果要保留实例变量中的每个文本字段,可以在委托方法中进行简单比较,以查看哪个字段已完成编辑。即使您通过使用关联数组从实例映射到字段名称来完全动态创建它们,也可以执行此操作,反之亦然。