Is there a function to set the section title of the tableview so that the title would not be uppercase?

Viewed 556

I created a tableview and set the section title. But the title is uppercase and I cannot find a way to make the title of the section normal. I know we can create a label to avoid the setting which makes the title capital but I wonder if there is a system method to deal with this problem.

Here is the old code:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return @"The Content";
}

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
    UITableViewHeaderFooterView *headerView = (UITableViewHeaderFooterView *)view;
    headerView.textLabel.font = [UIFont systemFontOfSize:12];
    headerView.textLabel.textColor = [UIColor lightGrayColor];
}

And thanks to Alok's answer,thought his solution is not what I want. But it remind me that we can actually edit the content in - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section so that the capitalization problem can be solved.

Here is the new code:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return @"The Content";
}


- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
    UITableViewHeaderFooterView *headerView = (UITableViewHeaderFooterView *)view;
    headerView.textLabel.font = [UIFont systemFontOfSize:12];
    headerView.textLabel.textColor = [UIColor lightGrayColor];
    headerView.textLabel.text = @"The Content";
}

What we need is to set the content of the tableview section again in - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section and the setting in - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section should be still.

1 Answers

But the title is uppercase and I cannot find a way to make the title of the section normal.

Yes that is by design.

Solution:

Implement below delegate method & it should solve your problem.

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    let titleView = view as! UITableViewHeaderFooterView
    titleView.textLabel?.text =  titleView.textLabel?.text?.capitalized
}
Related