Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change UITableView section header/footer WHILE RUNNING the app?

Tags:

I'm facing a problem I cannot resolve... I have a grouped table whose section header and section footer get displayed correctly upon launch thanks to

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 

and

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section 

Yet, I can't figure out how to update them later, I don't know how to change those texts and it's quite frustrating because in my mind it had to be nothing harder than changing a text label or whatever... (some ".text" property...)

I searched all through the documentation with no luck...

Any help is highly & kindly appreciated! Regards, Enrico

like image 513
Enrico Querci Avatar asked Oct 10 '09 09:10

Enrico Querci


People also ask

How do you add a footer in tableView?

To create a basic header or footer with a text label, override the tableView(_:titleForHeaderInSection:) or tableView(_:titleForFooterInSection:) method of your table's data source object. The table view creates a standard header or footer for you and inserts it into the table at the specified location.


2 Answers

In the delegate method, add a method call that returns the section name, e.g.:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {     switch (section) {         case firstSectionTag:             return [self firstSectionTitle];         case secondSectionTag:             return [self secondSectionTitle];         // ...         default:             return nil;     } }  - (NSString *)firstSectionTitle {     // generate first section title programmatically, e.g. "return [[NSDate date] description];"  }  // ... 

Then, when you need to update the section title, send an NSNotification that triggers something like the following method:

- (void)refreshTableSectionTitles:(NSNotification *)notification {     [tableView reloadData]; } 

If the table is large and you want finer control, pass an NSNotification with an NSDictionary that contains the section you want to reload, read the dictionary in -refreshTableSectionTitles to get back the section NSInteger, and use the table view's -reloadSections:withRowAnimation: to reload that specific section.

like image 106
Alex Reynolds Avatar answered Oct 13 '22 23:10

Alex Reynolds


You can use:

-(void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation 

And in

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 

define your new header text in statement.

This will make a nice animation while changing the header/footer.

like image 41
Ako Avatar answered Oct 13 '22 23:10

Ako