如何在自定义UITableView单元格上获取UITextField值

Bas*_*lba 2 uitableview uitextfield nsmutablearray ios

我是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]];
}
Run Code Online (Sandbox Code Playgroud)

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

Ell*_*eal 6

您需要参考您的实例UITextField,你在做什么有尝试调用textUITextField类.这样的事情可能会解决你的问题:

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];
}
Run Code Online (Sandbox Code Playgroud)


Him*_*dia 5

传递给您的委托的文本字段是单元格contentView的子视图.

- (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"];
      }

}
Run Code Online (Sandbox Code Playgroud)

在txtIndPath中,您将获得活动文本字段的索引路径.为textfield标签属性分配必要的值.最适合我,希望它也会帮助你.