Mehul Vasa Mehul Vasa - 2 years ago 91
Objective-C Question

How to get the value of textfield in a tableview cell on click of a button?

I have a tableview where user enters his social media profile urls. I want to get the value of that textfields on click of a button. The tableview is dynamic. I have attached the screenshot for reference. I am using Objective C to build up an iOs App and I am new to it. Any suggestions on how to do that? Thanks in advance.

[![This is the tableview with textfield in cell]

I have tried this :

NSMutableArray *Array = [NSMutableArray array];

for (int i=0; i < getMYurl.count; i++){

NSIndexPath *indexPath = [NSIndexPath indexPathForRow: i inSection: 0];

SHOWURLTableViewCell *cellS = [table cellForRowAtIndexPath:indexPath];

NSMutableDictionary *Dict = [NSMutableDictionary dictionary];

UITextField *TextName= (UITextField*)[cellS.contentView viewWithTag:indexPath.row];
}


And my cellForRowAtIndexPath Method :

- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
cellS = (SHOWURLTableViewCell *) [tableView dequeueReusableCellWithIdentifier:@"cell"];
// cellS.labelDesc.text = mergeName;


NSString *urlText = [[getMYurl valueForKey:@"url"]objectAtIndex:indexPath.row];
cellS.labelUrl.text = urlText;

NSString *urlName = [[getMYurl valueForKey:@"url_name"]objectAtIndex:indexPath.row];

// [urlMap setValue:mergeName forKey:urlText];

cellS.urlNameET.tag = 100;
}

Answer Source

You should have to store text of textfiled in any array variable (index wise) after end typing. Because tableview destroy the tableview cell while that was not showing into a screen.

- (void)textFieldDidEndEditing:(UITextField *)textField{       
          UITableViewCell *cell = (UITableViewCell*) textField.superview.superview;
          NSIndexPath *txtIndPath = [self.tblPendingDeliveryData indexPathForCell:cell];

          [myMutableArray insertObject:textField.text atIndex:txtIndPath.row];
    }

Don't forgot to set delegate to textfield

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download