iosobjective-cuitableviewuitextfieldnsstring

How to get UITextField's value into a NSString which is in UITableViewCell?


I set UITextField into UITableViewCell. Load the UITableView as per array count. All the textfields are editable. But, I want that, when I edit the UITextField then UITextField data should be stored into a predefined NSString. For each IndexPath.row's textfield is different string.

Attaching my code which I used to generate NSMutableArray...

[arrProfile removeAllObjects];
 arrProfile = [[NSMutableArray alloc] initWithObjects:
              @{@"title": @"CUSTOMER NAME", @"details": strFName},
              @{@"title": @"EMAIL ID", @"details": strEmail},
              @{@"title": @"MOBILE NO.", @"details": strContact},
              @{@"title": @"STATE", @"details": strStateName},
              @{@"title": @"CITY", @"details": @""},
              nil];

[_tblProfile reloadData];

Now attaching code used in cellForRowAtIndexPath...

static NSString *cellIdentifier = @"cellProfile";

NSDictionary *dictProfile = [arrProfile objectAtIndex:indexPath.row];
ProfileTableViewCell *cell = (ProfileTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

cell.lblTitle.text = [dictProfile objectForKey:@"title"];
cell.txtDetails.text = [dictProfile objectForKey:@"details"];

cell.txtDetails.delegate = self;
[self textFieldShouldReturn:cell.txtDetails];
[self textFieldShouldBeginEditing:cell.txtDetails];

return cell;

Solution

  • If I understand your question correctly, you want to build a dynamic form where a number of text fields will vary based on the array you are providing. You are trying to achieve this by taking table view and loading all text fields by each row. Now the problem comes up with how to take the value entered in each textfield. If this is your question here is the thing how to do it.

    You can do this by using Tag for textfield. You heared it correct!. In tableview cell method give each textfield unique tag (you can use indexpath.row) for that purpose. Assign delegate here. Textfield delegate methods will be fired. Now from textfield delegate methods again based on tag get the input from the textfield as a string and store.

    Note:

    [self textFieldShouldReturn:cell.txtDetails];
    [self textFieldShouldBeginEditing:cell.txtDetails];
    

    Methods not required to call from tableview cellForRowAtIndexPath. Textfield delegate if you assign, it will take care of it.