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!
You didn’t make it clear because you did not post enough code from your
cellForRowAtIndexPathmethod, but you are likely re-creating a UITextField each and every timecellForRowAtIndexPathis 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
cellForRowAtIndexPathis 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, likenameLabel).Take a look at this related StackOverflow question to see how to embed a UITextField inside a UITableViewCell.