This question should not be mixed up with this here.. These are two different things.
There is a good example how to use a UITableView Header on SO.
This all works fine and the main header is fixed on top as long as the style is set to plain.
But if I use sections, the main header no longer sticks to top and moves away while scrolling to the bottom.
In this method, I am returning the header for each section.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
In this method I am setting the height for the header section above:
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
In this method, I am setting the real table header.
- (void)viewWillAppear:(BOOL)animated {
...
self.recordTableView.tableHeaderView = headerView;
}
Is it even possible having a fixed table header, while using sections?
What is an alternative solution to this please?
If you want a UITableViewController (static cells/keyboard handling) and have a fixed header then you should use Containment. You can do this from a Storyboard by setting up a UIViewController with your fixed header and then using a Container View to embed the UITableViewController.
Once you have your containing view setup, you right-click drag from the Container View to the View Controller you want to embed - the UITableViewController in this case.
You can access and get a reference to the contained View Controller (the UITableViewController) from the Container View Controller by implementing the prepareForSegue:sender: method.
There’s no way to maintain the header of a tableView fixed, but
an useful approach when you need a unique header, is to use a UIViewController rather than a UITableViewController, and set the header (UIView) out from the tableView.
Something like this:
If you want to keep the class as a UITableViewController you can add your header as a subview to the tableview's superview. You will have to also push the tableview top inset down so your headerview doesnt hide the table.
Here is a sample code to put inside your tableViewController subclass (This example assumes your tableview controller is inside a navigation controller, so it pushes the view to below the navigation bar):
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.automaticallyAdjustsScrollViewInsets = NO;
}
-(void)addHeaderView{
CGFloat yPosition = self.navigationController.navigationBar.frame.origin.y + self.navigationController.navigationBar.frame.size.height;
mainHeaderView = [[UIView alloc] init];
const CGFloat mainHeaderHeight = 44;
[mainHeaderView setFrame:CGRectMake(0, yPosition, self.view.frame.size.width, mainHeaderHeight)];
mainHeaderView.backgroundColor = [UIColor redColor];
[self.tableView.superview addSubview:mainHeaderView];
[self.tableView setContentInset:UIEdgeInsetsMake(yPosition + mainHeaderHeight, self.tableView.contentInset.left, self.tableView.contentInset.bottom, self.tableView.contentInset.right)];
}
I haven't done this, but the first thing I would think to try is to place my tableview in a UIView and make my own header there in that UIView. Seems a trivial matter to make that view appear to be the header of the table and it would certainly stay put.
Related
I've created a view (with a searchbar and some buttons in it) to my UITableViewController. I've then created an outlet for the view in my ViewController.h file and synthasized it in my ViewController.m file.
Here's a screen shot of my current setup
I'm using
self.tableView.tableHeaderView = headerView;
And I'm using
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
return headerView;
}
But my view still scrolls up and down with the table, it doesn't seem to be set as the header properly?
"it doesn't seem to be set as the header properly" -- yes it is being set properly. That is how a table header behaves. If you don't want it to scroll, then you can do one of two things. Either add the view as a sibling view to a UIViewController (your view above, with the table view below), or, if you only have one section, you can use it as a section header which will stick to the top.
I'm using parse's PFQueryTableViewController, which is almost exactly the same as a UITableViewController except it handles some querying in the background.
My goal is to place a static toolbar at the bottom of the screen. I decided to do this by giving tableView a smaller frame, and creating and adding my toolbar subview below the tableView, as a subview to the same view that the tableView is a subview of.
Here's a photo. I am trying to add my toolbar to the little white space at the bottom of the screen. That whitespace does NOT scroll with the table view:
I have tried [self.view addSubview: toolbar], however, this just adds it to the tableView, and as a result, my toolbar scrolls with it. I can't seem to find any documentation on the view hierarchy for a UITableViewController. Anyone know where I should add my toolbar subview? I should also mention that all of this is sitting inside of a UINavigationController.
A PFQueryTableViewController (like all UITableViewControllers) has a table view as its view.
If you want to have other views within the view controller, either don't use a table view controller, or subclass UINavigationController.
Alternatively, you can set the view controller's toolbarItems property, and the navigation controller will take care of making and configuring a standard toolbar.
implement viewFAorHeaderInSection delegate method of UITableView:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 90)];
customView.backgroundColor = [UIColor blackColor];
return customView;
}
and heightForFooterInSection
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 80;
}
it should solove your problem.
In interface builder I have a header view above my UITableView, however in the simulator it is missing, and the Table View seems to be over it since it takes up most of the screen. Any reason for this? Work around?
Interface Builder
Simulator
In your case:
From your interface builder image I can see that table view is subview of view and you header view is also subview of tableview. It happening because tableview is hiding the header view. if you add the header view as subview of tableview then it would appear. But adding subview will not solve your problem because when you will scroll the tableview the subview will be gone.
For solving your problem as I can see that you have a navigation bar in your table view you can add the header view as subview of UINavigationBar.
[self.navigationController.view addSubview:headerView];
Good Practise:
Set the header view for section. Then you don't need to set any custom view at the top of your tableview. Try this:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *aView = [[UIView alloc] init];
//Customize the view according to our requirment
return aView;
}
Also implement this:
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 44.f; // Make this height as you need. Or else you may not see the full view.
}
Btw you can create a subclass of UITableViewController instead of UIViewController.
Hope this helps.. :)
Deselect Extend Edges / Under Top Bars
I have a UITableViewController that is in a storyboard. I want to add a header to it so I followed the instructions in the following post. I dragged a UIView up on top, and on top of that i dragged a couple of images.
Table Header Views in StoryBoards
Now that seemed to work fine, but the header scrolls along with the entries in table cells when i scroll up. Also, they seem to scroll underneath the time and battery indicator...not sure why that is.
So in the comments of that post i saw that you need to implement the following function to return the UiView to get it to "stick". The only way I saw how to get the UiView from the storyboard was to set the tag and then look it up, also shown below.
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section.
{
int tagNumber = 2;
UIView *headerView = (UIView *)[self.view viewWithTag:tagNumber];
return headerView;
}
The issue with all of this is the header still scrolls. How can i get the header view to just stay on top?
Thanks!
I've made header using this post: Table Header Views in StoryBoards
But i am unable to make header stick (fix) to the top of the screen while scrolling.
How can that be achieved?
P.S. Table style is plain, and when i tired to overload viewForHeaderInSection and returned outlet for header view, it just got worse.
Thanks in advance!
Here's the code which I believe makes the header view stick to the table top (that is not it's default behavior):
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGRect rect = self.tableHeaderView.frame;
rect.origin.y = MIN(0, self.tableView.contentOffset.y);
self.tableHeaderView.frame = rect;
}
An alternative is to have header view for the first section. You can't use any subview at viewForHeaderInSection, you have to return there the view which is not yet at views hierarchy. The advantage of the method is that the section header view is designed to be at the table top, the drawback is that you might want to have headers for sections in the feature, it will break the solution.
I think it's better for you to use a UIViewController, instead of a UITableViewController in the storyboard, to achieve this. Just put a header in the top of the view, in the storyboard, and put a UITableView under the header.
I was able to do it (at least in iOS 6) like this, don't know why this thing works:)
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if(self.headerManualView == nil) {
//allocate the view if it doesn't exist yet
headerManualView = [[UIView alloc] init];
//create the button
UITextField *txtField = [[UITextField alloc] initWithFrame:CGRectMake(10, 3, 250, 44)];
//the button should be as big as a table view cell
txtField.borderStyle = UITextBorderStyleRoundedRect;
//set action of the button
//[txtField addTarget:self action:#selector(removeAction:) forControlEvents:UIControlEventTouchUpInside];
//add the button to the view
[headerManualView addSubview:txtField];
}
//return the view for the footer
return headerManualView;
}