user843337
user843337

Reputation:

How to handle references to UITextFields in dynamic UITableView (iPhone)

I have a UITableView that is broken up into a user defined number of sections. Within each of these sections there are always 2 rows. Each of these two rows contains a UITextField which the user is able to edit.

What I need is a way of being able to access the data in these UITextFields at a later point. I was hoping it would be a simple problem however it's causing me a great deal of grief.

So far I have tried two approaches:

Attempt 1

I created two NSMutableArrays in which I added the UITextField objects to at index i (corresponding to the section it came from). I then tried to access the values by iterating through the array. This didn't work since the UITextFields kept getting wiped clean. Every-time I scroll down the table and the UITextField is out of view, when I go back it's contents have been wiped clean.

Attempt 2

I tried to get hold of the number of sections in the UITableView (this was fine). I then wanted to iterate through each section of the UITableView, recording the values in the rows of each. This is where I became unstuck since I'm not sure how to do this or if it's even possible.

I apologise if this is a naive question to ask, however I'm really struggling and would appreciate any advice.

Upvotes: 3

Views: 1470

Answers (3)

lnafziger
lnafziger

Reputation: 25740

Keep in mind that the text fields get reused as you scroll, so you don't really want to store references to them.

What you do instead, is to capture the information as it is entered. The easiest way to do this is to implement the textFieldDidEndEditing protocol method in the delegate.

The tricky part is figuring out which row the text field is in. The best way is to create a UITableViewCell subclass which has a NSIndexPath property. You can then set that when you configure the cell with tableview:willDisplayCell:forRowAtIndexPath:.

Then, in textFieldDidEndEditing, access the tableViewCell indexPath property through its superview. i.e.:

NSIndexPath indexPathOfParentCell = [(MyUITableViewCellSubclass *)self.superview indexPath]; 

Doing it this way allows you to know both the section and row of the cell.

Upvotes: 4

shabbirv
shabbirv

Reputation: 9098

Create your TextField in the cellForRow of the Table like so and give it a tag

    UITextField * userField = [[[UITextField alloc] initWithFrame:CGRectMake(0, 12, self.view.frame.size.width -20, 20)] autorelease];
    userField.tag = 1001;
    userField.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:14];
    userField.textAlignment = UITextAlignmentCenter;
    userField.delegate = self;
    userField.autocorrectionType = UITextAutocorrectionTypeNo;
    userField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    userField.clearButtonMode = UITextFieldViewModeWhileEditing;

    if (indexPath.row == 0)
        [cell.contentView addSubview:userField];  

then access the TextField like so:

    UITextField *userField = (UITextField *)[[(UITableViewCell *)[(UITableView *)tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]] contentView] viewWithTag:1001];

Upvotes: 1

Phillip Mills
Phillip Mills

Reputation: 31016

Your "Attempt 1" should work OK if you keep the text field's text in your arrays rather than the text field itself. (Anything that tries to make a view or control into a data object has a good chance of going wrong.)

Whatever acts as a data source for your table view should be able to re-populate the scrolled cells according to section and row if the content is stored separately.

Upvotes: 0

Related Questions