UITableView titleForHeaderInSection shows all caps

前端 未结 14 920
孤独总比滥情好
孤独总比滥情好 2021-01-30 16:28

I am using titleForHeaderInSection to show a header for a UITableView section. It worked fine with the iOS6 SDK, but the iOS7 SDK shows the header in all CAPS.

I guess

相关标签:
14条回答
  • 2021-01-30 17:23

    The solution which worked for me was to create a simple UILabel Instance Variable to use as the section header view. (Your needs may be different, but for me, I only needed to have text in my header...) Then, inside viewForHeaderInSection, I set up the label as required, and return this label as the header view.

    Then, whenever I want to update the text in the header, I simply change the label's text directly.

    In the .h file:

    @property (nonatomic, retain)  UILabel  *sectionHeaderLabel;
    

    then in the .m file:

    - (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        return [self    refreshMySectionHeaderLabel]; // I created this handy little method to update the header text
    }
    
    
    // Get the latest text for the section header...
    -(UILabel   *)  refreshMySectionHeaderLabel   {
        // Initialise the first time.    
        if( sectionHeaderLabel   ==  nil )   {
            sectionHeaderLabel   =   [[UILabel   alloc] initWithFrame:   CGRectNull];
        }
    
        // ...
        // May also set other attributes here (e.g. colour, font, etc...)
        // Figure out the text...    
        sectionHeaderLabel.text   =   @"Your updated text here...";
    
    
        return sectionHeaderLabel;
    }
    

    When I want to update my section header, I simply call:

    [self refreshMySectionHeaderLabel];
    

    ...or you could simply call:

    sectionHeaderLabel.text = @"The new text...";
    

    Take note: I only have 1 section (section 0). You may need to account for multiple sections...

    0 讨论(0)
  • 2021-01-30 17:24

    If you want to just keep the string as it is, you could simply do this:

    - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
        if ([view isKindOfClass:[UITableViewHeaderFooterView class]] && [self respondsToSelector:@selector(tableView:titleForHeaderInSection:)]) {
            UITableViewHeaderFooterView *headerView = (UITableViewHeaderFooterView *)view;
            headerView.textLabel.text = [self tableView:tableView titleForHeaderInSection:section];
        }
    }
    

    Swift solution:

    func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        if let headerView = view as? UITableViewHeaderFooterView, self.respondsToSelector(Selector("tableView:titleForHeaderInSection:")) {
            headerView.textLabel?.text = self.tableView(tableView, titleForHeaderInSection: section)
        }
    }
    
    0 讨论(0)
提交回复
热议问题