2

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

BastienPenalba
  • 158
  • 2
  • 10

3 Answers3

6

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];
}
Ell Neal
  • 6,014
  • 2
  • 29
  • 54
  • 1
    This works as long as you have declared that UITextField as a property named textField in your custom TableViewCell subclass. – LJ Wilson Jan 23 '12 at 01:13
5

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. It works well for me.

halfer
  • 19,824
  • 17
  • 99
  • 186
Himanshu padia
  • 7,428
  • 1
  • 47
  • 45
2

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{
   }
Community
  • 1
  • 1