再会!
我有一个UITableView
和两个单元格及其对应的UITextField
. 我通过for 循环创建了UITextField:
- (UITextField *)createInformationTextField:(NSString *)createInformationTextField createInformationPlaceholder:(NSString *)createInformationPlaceholder
{
for (int i = 0; i < 2; i++) {
informationTextField = [[UITextField alloc] initWithFrame:CGRectMake(120, 12, 170, 30)];
informationTextField.autocapitalizationType = UITextAutocapitalizationTypeWords;
informationTextField.autocorrectionType = UITextAutocorrectionTypeNo;
informationTextField.placeholder = createInformationPlaceholder;
informationTextField.text = createInformationTextField;
informationTextField.adjustsFontSizeToFitWidth = YES;
informationTextField.clearButtonMode = UITextFieldViewModeAlways;
informationTextField.tag = i;
}
return informationTextField;
}
我的cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSString *name;
NSString *address;
switch (indexPath.section) {
case 0:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
switch (indexPath.row) {
case 0:
informationTextField = [self createInformationTextField:name createInformationPlaceholder:[NSString stringWithFormat:@"%d", informationTextField.tag]];
cell.textLabel.text = @"Name";
[cell addSubview:informationTextField];
break;
case 1:
informationTextField = [self createInformationTextField:address createInformationPlaceholder:[NSString stringWithFormat:@"%d", informationTextField.tag]];
cell.textLabel.text = @"Address";
[cell addSubview:informationTextField];
break;
default:
break;
}
break;
case 1:
switch (indexPath.row) {
case 0:
cell.textLabel.text = @"State";
break;
default:
break;
}
break;
default:
break;
}
informationTextField.delegate = self;
return cell;
}
我将UITableView
占位符设置为显示其相应的标签,并且工作正常。
截屏:
问题出在我的textFieldDidEndEditing
:
- (void)textFieldDidEndEditing:(UITextField *)textField
{
switch (textField.tag) {
case 0:
NSLog(@"Name Field. The tag is %d", textField.tag);
break;
case 1:
NSLog(@"Address Field. The tag is %d", textField.tag);
break;
default:
break;
}
}
在用户在 中输入内容后,我尝试再次输出其标签,但这就是我所看到的UITextField
2013-06-06 17:36:53.050 UITableUITextField[20387:c07] Address Field. The tag is 1
2013-06-06 17:36:55.636 UITableUITextField[20387:c07] Address Field. The tag is 1