Reputation: 67
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;
Upvotes: 0
Views: 623
Reputation: 1
You can try use block function add to cell
cell.m_textChangeBlock = ^(NSString *text) {
if(indexPath.row){
weakSelf.m_models[indexPath.section].rtext = text;
}else{
weakSelf.m_models[indexPath.section].ltext = text;
}
};
Upvotes: 0
Reputation: 67
cell.txtDetails.delegate = self;
cell.txtDetails.tag = indexPath.row;
[cell.txtDetails addTarget:self action:@selector(textFieldDidEndEditing:) forControlEvents:UIControlEventTouchUpInside];
-(void)textFieldDidEndEditing:(UITextField *)textField {
if (textField.tag == 0) {
strFName = textField.text;
} else if (textField.tag == 2) {
strContact = textField.text;
}
[self loadTheMainArray];
}
Upvotes: 1
Reputation: 2043
Option 1:
That fits your implementation
Assign cell index to txtDetails.tag
while creating cell row (cellForRowAtIndexPath
).
This will help you in identifying your text field row
txtDetails.tag = indexPath.row
Implement UITextField delegate textFieldDidEndEditing
OR shouldChangeCharactersIn
to
And row index
arrProfile[textField.tag] = "new value"
Option 2:
Define a protocol to get cell text updates
protocol MYCellDelegate: class {
textFieldDidChange(row: Int, string: String)
}
class MyCell: UITableViewCell {
weak var delegate: MYCellDelegate?
var row: Int
}
In your cellForRowAtIndexPath do following
cell.delegate = self
cell.row = indexPath.row
Implement UITextFieldDelegate in cell (textFieldDidEndEditing
OR shouldChangeCharactersIn
)
and call textFieldDidChange(row: Int, string: String)
with appropriate values
Don't forget to remove following lines from cellForRowAtIndexPath
[self textFieldShouldReturn:cell.txtDetails];
[self textFieldShouldBeginEditing:cell.txtDetails];
Upvotes: 0
Reputation: 4174
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.
Upvotes: 1