How to set the UITableView Section title programmatically (iPhone/iPad)?

139,094

Solution 1

Once you have connected your UITableView delegate and datasource to your controller, you could do something like this:

ObjC

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

    NSString *sectionName;
    switch (section) {
        case 0:
            sectionName = NSLocalizedString(@"mySectionName", @"mySectionName");
            break;
        case 1:
            sectionName = NSLocalizedString(@"myOtherSectionName", @"myOtherSectionName");
            break;
        // ...
        default:
            sectionName = @"";
            break;
    }    
    return sectionName;
}

Swift

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

    let sectionName: String
    switch section {
        case 0:
            sectionName = NSLocalizedString("mySectionName", comment: "mySectionName")
        case 1:
            sectionName = NSLocalizedString("myOtherSectionName", comment: "myOtherSectionName")
        // ...
        default:
            sectionName = ""
    }
    return sectionName
}

Solution 2

If you are writing code in Swift it would look as an example like this

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?
{
    switch section
    {
        case 0:
            return "Apple Devices"
        case 1:
            return "Samsung Devices"
        default:
            return "Other Devices"
    }
}

Solution 3

Use the UITableViewDataSource method

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

Solution 4

titleForHeaderInSection is a delegate method of UITableView so to apply header text of section write as follows,

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

Solution 5

Note that -(NSString *)tableView: titleForHeaderInSection: is not called by UITableView if - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section is implemented in delegate of UITableView;

Share:
139,094

Related videos on Youtube

kaiz.net
Author by

kaiz.net

I'm a freelance developer working from home

Updated on July 08, 2022

Comments

  • kaiz.net
    kaiz.net almost 2 years

    I've created a UITableView in Interface Builder using storyboards. The UITableView is setup with static cells and a number of different sections.

    The issue I'm having is that I'm trying to setup my app in several different languages. To do this I need to be able to change the UITableView section titles somehow.

    Please can someone help me out? Ideally I'd like to approach the issue using IBOutlets however I suspect this isn't even possible in this case. Any advice and suggestions would be really appreciated.

    Thanks in advance.

  • drewish
    drewish almost 12 years
    You sure that actually gets called if you setup the story board using static cells? I doesn't seem like it's being invoked.
  • drewish
    drewish almost 12 years
    Ah it seems like you have to implement numberOfSectionsInTableView:tableView: to get it called.
  • wcochran
    wcochran about 11 years
    For static cells, (most) all the other data source methods are not implemented.
  • wcochran
    wcochran about 11 years
    @drewish numberOfSectionsInTableView:tableView: implemented in IB for static cells.
  • GreatWiz
    GreatWiz over 9 years
    drewish is right - if you implement numberOfSectionsInTableView: , then the title method is called and overries the storyboard. Since this is a static tableview then it's pretty ok to override it with a method that returns a constant number @wcochran
  • Blip
    Blip about 9 years
    But how do you dynamically change the header? How do you invoke this method?
  • BCI
    BCI over 8 years
    Just call the the reloadData() function from your UITableView instance. That way all data for your UITableView will be refreshed, this includes the invoke of the function from above.
  • l --marc l
    l --marc l over 3 years
    extension appears to not be needed with Swift 5.3. Xcode build messate: "'SettingsViewController' inherits conformance to protocol 'UITableViewDataSource' from superclass here"