我正在使用情节提要和动态 UITableView 添加我的单元格,如下所示。我需要知道如何在每个单元格的 UITextField 以及 UISwitch 状态等中保存此文本,这样当我上下滚动时,数据不会丢失。此外,当应用程序关闭时,我需要它来显示重新打开时输入的数据。我对编程很陌生,这是我的第一个 UItableView 项目。编码示例会很棒,请提供尽可能详细的信息!先感谢您!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
// Make cell unselectable and set font.
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.font = [UIFont fontWithName:@"ArialMT" size:13];
if (indexPath.section == 0) {
UITextField* tf = nil;
switch ( indexPath.row ) {
case 0: {
cell.textLabel.text = @"Name" ;
tf = nameFieldTextField = [self makeTextField:self.name placeholder:@"John Appleseed"];
nameFieldTextField.tag = 1;
[cell addSubview:nameFieldTextField];
break ;
}
case 1: {
cell.textLabel.text = @"Address" ;
tf = addressFieldTextField = [self makeTextField:self.address placeholder:@"Street Address"];
addressFieldTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
[cell addSubview:addressFieldTextField];
break ;
}
case 2: {
cell.textLabel.text = @"Email" ;
tf = emailFieldTextField = [self makeTextField:self.email placeholder:@"example@gmail.com"];
emailFieldTextField.keyboardType = UIKeyboardTypeEmailAddress;
[cell addSubview:emailFieldTextField];
break ;
}
case 3: {
cell.textLabel.text = @"Phone" ;
tf = phoneFieldTextField = [self makeTextField:self.phone placeholder:@"xxx-xxx-xxxx"];
phoneFieldTextField.keyboardType = UIKeyboardTypePhonePad;
phoneFieldTextField.tag = 10;
phoneFieldTextField.text = [self formatPhoneNumber:phoneFieldTextField.text deleteLastChar:YES];
[cell addSubview:phoneFieldTextField];
break ;
}
case 4: {
cell.textLabel.text = @"DOB" ;
tf = dateOfBirthTextField = [self makeTextField:self.dateOfBirth placeholder:@"xx/xx/xxxx"];
dateOfBirthTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
[cell addSubview:dateOfBirthTextField];
break ;
}
}
// Textfield dimensions
tf.frame = CGRectMake(120, 12, 170, 30);
// Workaround to dismiss keyboard when Done/Return is tapped
[tf addTarget:self action:@selector(textFieldFinished:) forControlEvents:UIControlEventEditingDidEndOnExit];