在问这个问题之前,我已经在Google上搜索了很多,却找不到合适的答案。
我有一个带有Three
部分和n
列数的tableView 。没有 每个节中的行数也不固定。最后两列包含一个UITextField
,最初每个文本字段中的值为0.00
。因此,在每行中输入值后,如果我关闭该部分,则textField值将还原为0.00
。谁能告诉我如何保存此textField值。
这是我的代码。
我正在使用自定义单元格,GenericTableViewCell
并self.columnHeaderArray
确定tableview中有多少列。columnHeaderArray值包含列详细信息,例如列类型,值等。
在cellForRowAtIndexPath
:
GenericTableViewCell *cell = nil;
if (cell == nil)
{
cell = [[GenericTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"withColumns:self.columnHeaderArray];
for (GenericTableColumn *theColumn in self.columnHeaderArray)
{
[self processDataForCell:cell forColumn:theColumn atIndexPath:indexPath];
}
return cell;
}
}
processDataForCell
是一种使用值和文本字段更新列的方法。在这种方法中,我检查列的类型并将值插入每列。
的代码是。
switch (column.columnType)
{
case textField:
{
UITextField *lblTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 4, label.frame.size.width - 10, label.frame.size.height - 6)];
lblTextField.delegate = self;
lblTextField.text = theValue; // setting the textField value iniatally 0.00
lblTextField.borderStyle = UITextBorderStyleRoundedRect;
lblTextField.layer.borderWidth = 1.0;
lblTextField.layer.borderColor = [[UIColor blackColor] CGColor];
[label addSubview:lblTextField];
break;
}
}
任何帮助将不胜感激。
编辑:感谢@Ayazmon和@naturalnOva的意见,我解决了这个问题,并带领我解决了这个问题。
编码:
在我的.h
文件中,我声明了一个NSMutableDictionary
名为textFieldValues
like
@property (nonatomic, strong) NSMutableDictionary <NSString *, NSString *> *textFieldValues;
在viewDidLoad
本例中,我将该字典实例化为
self.textFieldValues = [NSMutableDictionary new];
在processDataForCell
方法中,我将代码替换为
switch (column.columnType)
{
case textField:
{
UITextField *lblTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 4, label.frame.size.width - 10, label.frame.size.height - 6)];
lblTextField.delegate = self;
lblTextField.tag = indexPath.row;
if ([self.textFieldValues count] != 0) {
// pass in the indexPath from cellForRow
NSString *key = [NSString stringWithFormat: @"%ld-%ld", (long)indexPath.section, (long)indexPath.row];
lblTextField.text = self.textFieldValues[key];
} else {
lblTextField.text = theValue; // For setting the textField value iniatally 0.00
}
lblTextField.borderStyle = UITextBorderStyleRoundedRect;
lblTextField.layer.borderWidth = 1.0;
lblTextField.layer.borderColor = [[UIColor blackColor] CGColor];
[label addSubview:lblTextField];
break;
}
}
对我来说,UITextField delegate
方法textFieldShouldEndEditing(_:)
没有被调用。所以我用了textFieldShouldReturn
方法。
-(BOOL) textFieldShouldReturn:(UITextField *)textField
{
NSString *key = [NSString stringWithFormat: @"%ld-%ld", (long)indexPath.section, (long)indexPath.row];
self.textFieldValues[key] = textField.text;
return self.textFieldValues[key];
}
谢谢你们
当您滚动单元格的关闭视图时,它们将被释放。每次单元格滚动回视图时,都会重新初始化。如果您想保留文本字段的值,则可以保存它的内容,并保持该值不变。
之前,我坚持使用包含indexPaths值的Strings字典来完成此操作。每次文本字段辞职为第一响应者时,我都会将该字段的值存储在字典中。
您应该UITextField
在视图控制器中注册的委托,并覆盖textFieldDidEndEditing(_:)
可以访问文本字段值的位置。
例子:
为字段值创建一个属性:
@property (nonatomic, strong) NSMutableDictionary *textFieldValues;
覆盖UITextFieldDelegate
方法:
-(void) textFieldDidEndEditing: (UITextField *)textField
{
NSString *key = [NSString stringWithFormat: @"%ld-%ld", (long)indexPath.section, (long)indexPath.row];
self.textFieldValues[key] = textField.text;
}
使用上面的代码,将标签分配给文本字段,并使用字典分配字段内容:
UITextField *lblTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 4, label.frame.size.width - 10, label.frame.size.height - 6)];
lblTextField.delegate = self;
lblTextField.tag = indexPath.row;
// pass in the indexPath from cellForRow
NSString *key = [NSString stringWithFormat: @"%ld-%ld", (long)indexPath.section, (long)indexPath.row];
lblTextField.text = self.textFieldValues[key];
lblTextField.borderStyle = UITextBorderStyleRoundedRect;
lblTextField.layer.borderWidth = 1.0;
lblTextField.layer.borderColor = [[UIColor blackColor] CGColor];
[label addSubview:lblTextField];
希望能有所帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句