3

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.

3 Answers3

4

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.

lnafziger
  • 25,760
  • 8
  • 60
  • 101
  • Thanks. I really like this idea, however I'm having an issue in that I've set the `indexPath` on the subclassed `UITableviewCell`, however when `textFieldDidEndEditing` gets called I have no way of accessing the `indexPath` (since its a `UITextField` that gets passed in, not a `UITableViewCell`). Do you have any advice? –  May 30 '12 at 17:47
  • Yeah, I was in a hurry when I wrote this, sorry. You either need to access the superview of the textfield, or subclass the textfield instead and then in willDisplayCell, iterate through all of the subviews of your table view cell and set the indexPath of each text field. – lnafziger May 30 '12 at 18:00
  • 2
    Thanks for your help. I ended up subclassing the UITextField and adding the NSIndexPath on that instead of on the UITableViewCell. Everything works as desired now. Thanks again. –  May 30 '12 at 18:04
1

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];
shabbirv
  • 8,958
  • 4
  • 33
  • 34
0

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.

Phillip Mills
  • 30,888
  • 4
  • 42
  • 57