Pregunta

I'm facing a problem with UITableView and it's property tableHeaderView.

I want to have the tableHeaderView to behave like UISearchBar, i.e. the content offset should be the of tableHeaderView. Setting contentOffset, etc. didn't help when the table view wouldn't fill the view's frame.

Here's a screenshot of the current behavior:

current behavior
(source: tubtub.de)

And how I'd like it to have:

desired behavior
(source: tubtub.de)

I'm inserting the headerView in viewDidLoad as follows:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    headerView.backgroundColor = [UIColor redColor];
    self.tableView.tableHeaderView = headerView;
}

Any help or hint is highly appreciated. Thanks!

EDIT: I made it working by utilizing UIScrollViewDelegate and subclassing UITableView. Check it out on the github repo provided.

¿Fue útil?

Solución 2

I came up with implementing my own solution.

I utilize UIScrollViewDelegate and came up with a subclass of UITableView for calculating the contentSize dynamically.

Take a look at the github repo here.

Otros consejos

You can use setContentOffset: by headerView height. This contentOffset will only happen when you have enough number of data to be off by your headerView height. i.e) If tableView is not scrollable because you have only few data like one in your screenshot, the headerView is still visible. However if you have lots of number of data that can't not be displayed in the screen it will have contentOffset.

Try this with 20 rows. You will see what I mean.

UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
headerView.backgroundColor = [UIColor redColor];
self.tableView.tableHeaderView = headerView;
[self.tableView setContentOffset:CGPointMake(0, 44)];

With only 3 rows, this contentOffset won't work and it might be desired behavior in your case because you don't want to hide the searchBar when you have extra space to display.

I would double check the frame for the table itself. By setting the headerView's frame to start at 0,0, you are specifying that it should have its origin in the top left of the the view that is designated for the table.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top