简体   繁体   中英

Programmatically add UITextView as UITableView header

I am trying to add a TextView as the header of a table view. My code is below. The code runs, as I see the log message printed. But there is no header in my table/scene. Does anyone know why the header is not showing? I am making the call to show/add the header inside cellForRowAtIndex as

if (!tableView.tableHeaderView) {
    NSLog(@"adding header to table");
    [self addHeaderTableView:tableView ];
}

The actual code for adding the header is this:

-(void)addHeaderToTableView:(UITableView *)tableView
{
    UITextView *headerView = [[UITextView alloc] init];
    headerView.text = @"some text";
    tableView.tableHeaderView = headerView;
    NSLog(@"Added textview to table as header");
}

Again, the logs are behaving well.

You forgot the line

[headerView sizeToFit];

after adding the view to the table as header.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM