【问题标题】:How to save text entered in UITextField contained in a UITableViewCell?如何保存在 UITableViewCell 中包含的 UITextField 中输入的文本?
【发布时间】:2015-04-14 07:22:01
【问题描述】:

在我的应用程序中,我正在动态创建一个带有 2 个控件的表格视图单元格,一个是UILabel,用于显示我从 Web 服务接收的标题,另一个是 aUITextField。这个UITextField 将接受用户输入。

现在所有的字段名称和字段类型都是从网络服务接收的,所以我不知道每次都会来哪个字段?

那么,当用户在字段中输入相应的值时,我如何保存它们并识别它们属于哪个标题?

我尝试了this 选项,但它仅适用于静态UITableView(他们已经知道哪些字段属于值)

我将得到一系列字段,例如“身高”、“血压”等。 我的用户界面是这样的

在我的代码部分

.m file is 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;    //count of section
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [fieldName count]; //dynamic field name array count

}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *MyIdentifier = @"cell";

    cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    if (cell == nil)
    {
        cell = [[testCell alloc] initWithStyle:UITableViewCellStyleDefault
                                             reuseIdentifier:MyIdentifier];
    }

    NSString *uppercaseString = [[fieldName objectAtIndex:indexPath.row] uppercaseString];
    cell.title.text=uppercaseString;//filed name

    return cell;
}

从这里我如何获取输入文本值并保存它???

【问题讨论】:

  • 您想节省任何操作或点击的价值吗??
  • 使用数据源保存值。
  • 是的,当我点击按钮时,我需要保存值
  • 正如 iphonic 所说,您有一个数据源。使用它!
  • 代码或设计中的 UITextField 和保存按钮在哪里???

标签: ios objective-c iphone uitableview tableviewcell


【解决方案1】:

我推荐你使用FXForms,它是为动态表格视图设计的。它使用键值编码模式保存和获取表单模型数据。

【讨论】:

    【解决方案2】:

    如果从表单字段中获取值并存储它们是您的问题,请尝试这种方法。我仍然不确定您到底在寻找什么。

    将表单字段对象添加到 NSMutableArray。单击完成按钮时,遍历数组并获取相应的字段对象和用户从中输入的值。

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
    return 1;    //count of section
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [fieldName count]; //dynamic field name array count
    
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *MyIdentifier = @"cell";
    
         if (!self.fieldsArray) {
            self.fieldsArray = [[NSMutableArray alloc]init];
         }       
    
        cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    
        if (cell == nil)
        {
            cell = [[CustomCell alloc]init]
        }
    
        NSString *uppercaseString = [[fieldName objectAtIndex:indexPath.row] uppercaseString];
    
        cell.titleLabel.text=uppercaseString;//filed name
    
    
         UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 30, 250, 250)];
         [cell.contentView addSubview:textField];
         [self.fieldsArray addObject:textField];    
    
        }
    
        return cell;
    }
    
    //Button Click
    -(IBAction)doneSelection:(id)sender{
        for (id *field in _testArray) {
            if ([field isKindOfClass:[UITextField class]]) {
                 NSString* userEnteredValue = field.text;
            }else if ([field isKindOfClass:[UISlider class]]){
               //Handle for other types
            }
        }
     }
    

    【讨论】:

    • 我的问题很清楚,请不要发布这种答案,完全没有意义
    • 很抱歉让您感到厌烦。我只是想帮忙。没有其他目的。
    • 你没有理解确切的问题.....动态意味着首先没有硬编码!但你仍然硬编码“高度”!!!
    • @Abhi,这只是一个示例代码。请看一下这个概念。您可以根据自己的需要进行自己的实现。请修改我的代码并检查。请不要期望共享完整的代码。
    • @abhibangalore 如何做一个正派的人,而不是对所有试图提供帮助的人大喊大叫?
    【解决方案3】:

    您可以使用相同的值标记每个标签和文本字段,假设 row1 将具有标记值 1,而 row2 将具有标签和文本字段将具有标记值 2,因此您可以轻松识别该对并获取文本。像这样修改你的方法

    - (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *MyIdentifier = @"cell";
    cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    
    if (cell == nil)
    {
        cell = [[testCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
    }
    UILabel *lblTilte=[[UILabel alloc]init];
    [lblTilte setFrame:<#(CGRect)#>];//set your own frame
    NSString *uppercaseString = [[fieldName objectAtIndex:indexPath.row] uppercaseString];
    [lblTilte setText:uppercaseString];//your title received from web service
    lblTilte.tag=indexPath.row;//identify pair
    [cell.contentView addSubview:lblTilte];
    
    UITextField *inputTextField=[[UITextField alloc]init];
    [inputTextField setFrame:<#(CGRect)#>];//set your own frame
    inputTextField.tag=indexPath.row;//identify pair
    [cell.contentView addSubview:inputTextField];
    return cell;}
    

    现在,当您收到输入时,将其与标签匹配。您可以在文本字段委托方法中获取输入值,文本字段已结束编辑,或者如果您有单元格选择方法,您也可以检索这些值。

    【讨论】:

      【解决方案4】:

      要在本地保存数据,您可以使用 .Plist(Property List)。

      viewWillAppear方法中创建NSMutableArray并重新加载UITableView

      .Plist 是存储数据或保存数据的最佳方式。 或者 你也可以使用它的核心数据。

      或者,如果您的数据量非常少,那么您可以使用 NSUserDefaults。

      【讨论】:

      • 请尝试以上答案。它会解决你的问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多