ecnepsnai
ecnepsnai

Reputation: 2026

When setting TableViewHeader, the section header text is removed

When I programatically set the headerView for a UITableView, any sections will lose their section header text.

I'm just using this code to set the header after the view loaded (and a REST request has been made)

/* In the request response method */
self.tableView.tableHeaderView = self.alertView;
shouldShowBanner = YES;
[self.tableView reloadData];

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if(shouldShowBanner){
        return self.alertView;
    } else {
        return nil;
    }
}

I create the view programmatically depending on the response I get from the server. If I don't show a custom header, the section title works normally (as I set it in the Storyboard). But as soon as it shows the section title goes away.

I tried using this method found in the delegate protocol

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

It's called, but doesn't seem to do anything. Just to be clear, the view that I am creating does show without any problems.

Upvotes: 0

Views: 81

Answers (1)

rmaddy
rmaddy

Reputation: 318954

A view can only have one parent. It (self.alertView) can't be in both the the table header view and the section header view.

Create separate views for both.

Also, do not implement both titleViewHeaderInSection and viewForHeaderInSection. Just one or the other. And if you do implement viewForHeaderInSection make sure you also implement heightForHeaderInSection too.

Upvotes: 1

Related Questions