我在 UITableviewController
中有一个文本框。我想在用户完成输入并移至下一个字段后获取用户输入的数据。我想存储该信息。
因为一旦我滚动,用户输入的数据就会消失。
感谢任何帮助。
if (([indexPath section] == 1) && ([indexPath row] == 2))
{
static NSString *CellIdentifier1 = @"Cell";
GeneralInfocell *cell = (GeneralInfocell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
[[NSBundle mainBundle] loadNibNamed:@"GeneralInfocell" owner:self options:nil];
cell = addresscell;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
addresscell.txtStreet.delegate = self;
addresscell.txtStreet.font = [UIFont fontWithName:@"Arial" size:14];
addresscell.txtStreet.keyboardType = UIKeyboardTypeDefault;
addresscell.txtStreet.returnKeyType = UIReturnKeyNext;
addresscell.txtStreet.backgroundColor = [UIColor whiteColor];
addresscell.txtStreet.autocorrectionType = UITextAutocorrectionTypeNo;
addresscell. txtStreet.autocapitalizationType = UITextAutocapitalizationTypeNone;
addresscell. txtStreet.textAlignment = UITextAlignmentLeft;
addresscell.txtStreet.textColor = [UIColor blackColor];
[addresscell.txtStreet addTarget:self action:@selector(textFieldDidEndEditing:) forControlEvents:UIControlEventEditingDidEnd];
}
在我的 textFieldDidEndEditing
方法中,
- (void)textFieldDidEndEditing:(UITextField *)textField {
UILabel *label = [NSString stringWithFormat:@"%@",[addresscell.txtCity text]];
NSLog(@"label:%@",label);
}
但我没有得到任何值(value)。
谢谢
最佳答案
您的 UITextFieldDelegate
应该是这样的 –
- (void)textFieldDidEndEditing:(UITextField *)textField {
NSLog(@"Data entered: %@", textField.text);
}
由于其他一切都已正确连线,您现在应该获得正确的值。
由于单元格被重复使用,您会在滚动时丢失数据。每次请求单元格时,您都需要重置文本字段的值。为此,如果有多个字段,您可以维护一个可变的字符串数组,每个文本字段一个。当文本字段被关闭时,保存在数组中输入的数据并在下次请求单元格时恢复它。
如果您需要任何进一步的帮助,请告诉我。
编辑
标记
所有十个文本字段 0 到 9。
在您的 View Controller 中声明一个 NSMutableArray iVar,例如 data
,以存储文本字段的字符串。
在-viewDidLoad:
,
...
data = [[NSMutableArray] initWithCapacity:10];
for ( int i = 0; i < 10; i++ ) {
[data addObject:@""];
}
...
在 -textFieldDidEndEditing:
,
...
[data replaceObjectAtIndex:[textField tag] withObject:[textField text]];
...
在 -tableView:cellForRowAtIndexPath:
,
...
// Since they are named variables, it will be something on lines of
addressCell.txtStreet.text = [data objectAtIndex:[addressCell.txtStreet tag]];
// Repeat for all other text fields
...
不要忘记在dealloc
方法中释放data
。
关于iphone - UITableView 中的 UITextfield 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6024645/