【问题标题】:How to get UITextField value on custom UITableView cell如何在自定义 UITableView 单元格上获取 UITextField 值
【发布时间】:2012-01-22 23:44:14
【问题描述】:

我是 IOS 开发新手,我正在尝试开发我的第一个应用程序。

所以我的问题是...我有一个带有自定义单元格的 UITableView,每个单元格都包含一个 UITextField。当我按下按钮时,我想将每个 UITextField 值放在 NSMutableArray 中。

我想我必须做类似的事情,但我不确定:

NSMutableArray *playerNameArray = [[NSMutableArray alloc] init];

for (int i=0; i < nbPlayers; i++){ //nbPlayers is the number of rows in the UITableView
    NSString *playerName =UITextField.text;
    [playerNameArray addObject:[NSString stringWithFormat: @"%@", playerName]];
}

如果有人可以帮助我.... :) 谢谢

【问题讨论】:

    标签: ios uitableview nsmutablearray uitextfield


    【解决方案1】:

    您需要引用您的UITextField 的实例,您在那里所做的是尝试在UITextField 类上调用text。这样的事情可能会解决您的问题:

    NSMutableArray *playerNameArray = [[NSMutableArray alloc] init];
    
    for (int i=0; i < nbPlayers; i++){ //nbPlayers is the number of rows in the UITableView
    
        MyTableViewCellSubclass *theCell = (id)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
        UITextField *cellTextField = [theCell textField];
    
        NSString *playerName = [cellTextField text];
        [playerNameArray addObject:playerName];
    }
    

    【讨论】:

    • 只要您在自定义 TableViewCell 子类中将 UITextField 声明为名为 textField 的属性,此方法就可以工作。
    【解决方案2】:

    传递给您的委托的文本字段是单元格内容视图的子视图。

    - (BOOL)textFieldShouldEndEditing:(UITextField *)textField
    {
          UITableViewCell *cell = (UITableViewCell*) textField.superview.superview;
          NSIndexPath *txtIndPath = [self.tblPendingDeliveryData indexPathForCell:cell];
        
          write you code here.....like
          if(textField.tag == 1)
          {
              NSMutableDictionary *dict = [self.arrPendingDeliveryData objectAtIndex:txtIndPath.row];
              [dict setObject:textField.text forKey:@"ReOrder"];
          }
    
    }
    

    在 txtIndPath 中,您将获得活动文本字段的索引路径。为 textfield 标签属性分配必要的值。对我来说效果很好。

    【讨论】:

      【解决方案3】:

      查看此主题以了解类似问题

      UITextField in UITableViewCell Help

      您应该使用其中一种文本字段委托方法来填充数组的正确单元格

       - (void)textFieldDidEndEditing:(UITextField *)textField{
         }
      

      【讨论】:

        猜你喜欢
        • 2018-01-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多