3

I have a static tableView with some custom cells. What I wan't to do is change the section titles programmatically. As far as I know, because the cells are static, I can't use methods like cellForRowAtIndexPath and so on, so my question is, is it possibly to change them like.

self.tableView.section1.text = @"title1"; // something like this?

I've tried to create an IBOutlet of the section but I get the following error:

Unknown type name 'UITableViewSection': did you mean 'UITableViewStyle?'

What I can do is edit the content of the cells, but not the header.

Thanks!

Linus
  • 4,643
  • 8
  • 49
  • 74

3 Answers3

6

Use viewForHeaderInSection method.

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {


        UILabel *label1 = [[UILabel alloc] init];
        label1.frame = CGRectMake(0, 0, 190, 20);
        label1.textColor = [UIColor blackColor];
        // label1.font = [UIFont fontWithName:@"Helvetica Bold" size:16];
        [label1 setFont:[UIFont fontWithName:@"Arial-BoldMT" size:14]];
        label1.textAlignment = UITextAlignmentCenter;

        label1.text =[NSString stringWithFormat:@"Title %d",section];
// If your title are inside an Array then Use Below Code 

       label1.text =[titleArray objectAtindex:section];

        label1.textColor = [UIColor whiteColor];
        label1.backgroundColor = [UIColor clearColor];




UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
    [view addSubview:label1];

    view.backgroundColor = [UIColor orangeColor];
         return view;

    }

if You want to use titleForHeaderInSection then use below code.

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

return [NSString stringWithFormat:@"Title %d",section];
// If your title are inside an Array then Use Below Code 

       return [titleArray objectAtindex:section];
}
Siba Prasad Hota
  • 4,779
  • 1
  • 20
  • 40
3

You can use the UITableViewDelegate Protocol method tableview:titleForHeaderInSection:

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

    NSString *sectionTitle = @"";

    switch (section) {

        case 0: sectionTitle = @"Section 1"; break;
        case 1: sectionTitle = @"Section 2"; break;
        case 2: sectionTitle = @"Section 3"; break;

        default:
            break;
    }

    return sectionTitle;
}

Make sure you declare your <UITableViewDelegate> in your .h file:

@interface SettingsViewController : UITableViewController <UITableViewDelegate> {

}
jhilgert00
  • 5,479
  • 2
  • 38
  • 54
  • This works well as long as I set up the section headings before the view loads. However, I'm hoping to change the table headings in response to user actions on the table. Any way to force the table to reload or something to reflect these changes (assuming I set a sectionTitle to a local property value that changes)? – Nick Jan 20 '15 at 08:59
  • 1
    Figured it out: much easier to do it like this: `[self.tableView headerViewForSection:1].textLabel.text = @"blah";` Via http://stackoverflow.com/a/17959411/1304462 – Nick Jan 20 '15 at 09:15
2

If you need to change the headers when the view is displayed 'live' you can do so like this:

int sectionNumber = 0;
[self.tableView headerViewForSection:sectionNumber].textLabel.text = @"Foo Bar";

But doing so doesn't seem to change the size of the label frame. I just made mine larger in advance. More details here.

Community
  • 1
  • 1
Nick
  • 3,172
  • 3
  • 37
  • 49