Nono Nono - 1 month ago 11x
iOS Question

Objective-C: Get text field values of each table row

I have a table view with customs cells. Each cell got two text fields. Below the table is a button. I have a "buttonPressed" action in the table view controller. How can I get the text field values of each table row in this method?

Here is the custom cell header file:


@interface CustomCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UITextField *name;
@property (weak, nonatomic) IBOutlet UITextField *age;


And in the table view controller I have this method:

- (IBAction)saveBtnPressed:(id)sender{
// Store the text field values of each row in an array here

Can someone help me?


According to ios design pattern you need to store value of textfield in array in delegate of custom cell. But for your example if you want all textfield value as same place then below is solution for you. Assuming that saveBtnPressed action is in viewController(not in ). Assuming there are 5 rows.

- (IBAction)saveBtnPressed:(id)sender{

       NSMutableArray *userDetails = [[NSMutableArray alloc] init];
           CustomCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
           NSDictionary *userDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:, @"name", cell.age.text, @"age", nil];
          [userDetails addObject:userDictionary];

userDetailsArray will have details of all cells.