UITextField上的值滚动变化

杰斯汀·萨吉·查克

在问这个问题之前,我已经在Google上搜索了很多,却找不到合适的答案。

我有一个带有Three部分和n列数的tableView 没有 每个节中的行数也不固定。最后两列包含一个UITextField,最初每个文本字段中的值为0.00因此,在每行中输入值后,如果我关闭该部分,则textField值将还原为0.00谁能告诉我如何保存此textField值。

这是我的代码。

我正在使用自定义单元格,GenericTableViewCellself.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名为textFieldValueslike

@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];

}

谢谢你们

naturaln0va

当您滚动单元格的关闭视图时,它们将被释放。每次单元格滚动回视图时,都会重新初始化。如果您想保留文本字段的值,则可以保存它的内容,并保持该值不变。

之前,我坚持使用包含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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

上滚动变化侧条的<LI>色

来自分类Dev

滚动RecyclerView时,自定义视图值不断变化

来自分类Dev

在选择的UITextField上滚动UIScrollView,无法获得正确的大小

来自分类Dev

UITextField光标位置随方向变化而变化

来自分类Dev

滚动左边距变化

来自分类Dev

滚动角度变化

来自分类Dev

如何在UIButton单击上更改UITextfield的值?

来自分类Dev

图像在滚动和fullPage.js上发生变化

来自分类Dev

在 Swift4 上通过键盘隐藏时如何滚动 UITextField?

来自分类Dev

位置recylerview变化,同时滚动

来自分类Dev

jQuery图像高度滚动变化

来自分类Dev

jQuery:滚动宽度变化平滑

来自分类Dev

滚动时如何在屏幕上附加div偏移值?

来自分类Dev

RecyclerView上/下滚动更改错误的值

来自分类Dev

检测textarea值变化

来自分类Dev

取最后的变化值

来自分类Dev

观测值何时变化?

来自分类Dev

HashMap的值如何变化?

来自分类Dev

即使焦点在其他对象上,也可以在滚动时更改滚动条的值

来自分类Dev

更改SCNText上的字符串值不会产生任何变化

来自分类Dev

在WooCommerce单个产品上动态设置和显示自定义产品变化值

来自分类Dev

SQL:我需要计算与限定符相同的上一行相比,值的变化

来自分类Dev

在SQL中计算当年和上一年同期的值的百分比变化

来自分类Dev

滚动动画时UITableView的高度变化

来自分类Dev

数据表变化窗口的滚动总和

来自分类Dev

如何制作随滚动而变化的垂直滑块?

来自分类Dev

滚动动画时UITableView的高度变化

来自分类Dev

滚动方向在最近升级后不断变化

来自分类Dev

UITableView滚动条的大小随用户滚动而变化