How to get UITextField value on custom UITableView cell

I'm new to IOS development and i'm trying to develop my first app.

So my problem is... I've got a UITableView with custom cells, each cell contains an UITextField. When I press a button I'd like to put each UITextField value in a NSMutableArray.

I think I've got to do something like that but I'm not sure:

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

If someone can help me.... :) Thanks

Answers


You need to reference the instance of your UITextField, what you're doing there is attempting to call text on the UITextField class. Something like this would probably solve your problem:

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

The text field passed to your delegate is a subview of the cell's 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"];
      }

}

In txtIndPath you will get active textfield's indexpath. assign the textfield tag property with necessary value. works best for me, hope it will also help you.


Look at this thread for a similar question

UITextField in UITableViewCell Help

You should to use one of the textfield delegated method to fill the proper cell of your array

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

Need Your Help

How to align MATLAB subplots?

matlab matlab-figure subplot

My MATLAB subplots are not aligned perfectly, see the green rectangle in figure below. How to avoid this shift?

Z-Index Preventing Clicking on Navbar Link

jquery html css css-position z-index

Can the z-index or positioning of an element prevent a user from clicking on a navbar link?