0

I have a grouped table with 3 categories and one header above the first one . I would like that after the user types in his/her name , the header title to update itself with that person's name . I've put the reloadData method in textFieldShouldReturn after I dismiss the first responder ( keyboard ) . It does not seem to work though . The header title remains the same .

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if(section == 0 ) 
    {
        if(title==nil)
            return @"Your name here...";
        else
            return title;
    }
    else return nil;
}

title is a NSString which should contain the text from the title cell.

The following code is from cellForRowAtIndexPath....

UITextField *txt = [[ UITextField alloc ] initWithFrame:CGRectMake(100,10, 200, 30)];``
    txt.delegate = self;
    [cell addSubview:txt];

    if(indexPath.section==0)
    {
        if([indexPath row] == 0)
        {
            [cell.textLabel setText:@"Name"];
            title = txt.text;

        }

Thanks!

Teo
  • 3,246
  • 9
  • 37
  • 70
  • Could you modify your original question to show where / how that `UITextField * txt = ...` initializer is being called? Right now I suspect you are constantly and repeatedly calling that alloc/init method and therefore your `title` variable is always set to NULL. – Michael Dautermann Jan 05 '12 at 11:32
  • the `reloadData` reloads only the table cells or the whole table including the headers and footers ? – Teo Jan 05 '12 at 11:37

1 Answers1

0

You didn't make it clear because you did not post enough code from your cellForRowAtIndexPath method, but you are likely re-creating a UITextField each and every time cellForRowAtIndexPath is being called (which is a lot). This also means you're leaking memory like crazy (and your app would get rejected from being listed on the app store, to boot).

To fix this, create a UITableView cell in your xib file and embed a UITextField into it (connect IBOutlets to both), then return the UITableView cell when cellForRowAtIndexPath is called for the index path where you want the text field to appear.

Alternatively, you can create a UITableView cell programatically (not in a xib), initialize the UITextField as you were doing up there and assign that to a variable in your class.

In any event, you only want to alloc & init a UITextField once and only once. If you can do that, you can then call title = txt.text; (you should also rename your UITextField to something more intuitive, like nameLabel).

Take a look at this related StackOverflow question to see how to embed a UITextField inside a UITableViewCell.

Community
  • 1
  • 1
Michael Dautermann
  • 86,557
  • 17
  • 155
  • 196
  • Okay , I understand your suggestions but still , how can I change the header's title while the app is running ? After I insert the name , I would like the header to change it's text... – Teo Jan 05 '12 at 11:59
  • Calling `reloadData` should also refresh the header ([this related question seems to indicate that fact](http://stackoverflow.com/questions/2805326/prevent-header-from-being-updated-after-calling-reloaddata))... so simply call `reloadData` after the name is entered (which you can detect via the text field delegate method [`textFieldDidEndEditing:`](http://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html#//apple_ref/occ/intfm/UITextFieldDelegate/textFieldDidEndEditing:)) – Michael Dautermann Jan 05 '12 at 12:03