Change UITableView section header/footer WHILE RUNNING the app?

后端 未结 4 647
梦谈多话
梦谈多话 2020-12-24 13:48

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

- (NSS         


        
相关标签:
4条回答
  • 2020-12-24 13:56

    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.

    0 讨论(0)
  • 2020-12-24 13:57

    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.

    0 讨论(0)
  • 2020-12-24 14:02

    Use this line in your code where you want to change the title:

        [[self theTableViewToChangeItsHeader]reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
    
    0 讨论(0)
  • 2020-12-24 14:07

    In Your TableViewController use

    [self.tableView reloadSectionIndexTitles];
    
    0 讨论(0)
提交回复
热议问题