iOS 7, table header view does not float - ios

I have a UITableView set up on my app, which runs on iOS 7. I has one section and it loads images into custom cells and it scrolls under the navigation bar as well, which is translucent. So initially, the content is below the navbar and it scrolls under the navbar as we scroll down to view more images. For this I have set an initial contentInset of UIEdgeInsetsMake(40, 0, 0, 0). Now sometimes, I need a small header view on my table to indicate types of images on my table. So I have used the following code:
-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 30.0;
}
-(UIView*) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
TableSectionHeader *header=[[[NSBundle mainBundle] loadNibNamed:#"TableSectionHeader" owner:self options:nil] objectAtIndex:0];
[header.title setText:[NSString stringWithFormat:#"Type: %#", self.imageType]];
return head;
}
Where TableSectionHeader is custom view I have created for this purpose. Now ideally, the header must float or "stick" either just below the navbar or at the top of the table (which is under the navbar). But in this case, it just rolls off screen. I want the header to stick right under the navbar. Does anyone know how I can achieve this?

Change the table view's style from Grouped to Plain.
From the official documentation, regarding the Plain table view style:
A plain table view can have one or more sections, sections can have
one or more rows, and each section can have its own header or footer
title. (A header or footer may also have a custom view, for instance
one containing an image). When the user scrolls through a section with
many rows, the header of the section floats to the top of the table
view and the footer of the section floats to the bottom.

Related

Issue with top table view cell being hidden or having spacing

I've had some issues with my table view and the top cell. I'm using grouped Prototype Cells, and I've come across an issue with the spacing between the top bar and the first cell. As I've seen on other posts, I tried using 'adjust scroll insets' however, this created another problem, with the cell being hidden underneath the navigation bar. When I try changing the translucency of the navigation bar, the spacing returns. I've got some links to the images below.
When you use grouped cell, it's make space automatically in the header section. Try that;
-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
if(section == 0)
return 5.0;
return 1.0;
}
It's because that you're using Grouped Style. people use this style to add their header for each group.
You should change the style to plain.
In xib, you should change the Style to Plain
Or do this programmaticlly when init the UITableView
UITableView *myTable = [[UITableView alloc] initWithFrame:CGRectZero
style:UITableViewStylePlain];

iOS viewForHeaderInSection covering other cells

I am trying to implement a header view in my application and have the following code to create the header view:
(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[[NSBundle mainBundle] loadNibNamed:#"FutureAppointmentsHeaderView" owner:self options:nil] lastObject];
return view;
}
I have also set the height of the headerView as follows:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 70;
}
When the page loads, the header view is in the correct position (see first image). However when I scroll the header covers the other cells (see second picture.)
I can't what I am doing wrong to get this behaviour. Any help much appreciated.
This doesn't sound like a wrong behavior. This is how the header should act. Take, for example, the Contacts app on your iPhone - when you scroll, the header with the current letter always snaps to the top. When you continue scrolling, it is replaced by the next header in line.
What you should do is add a background color to the header instead of leaving it transparent. I think everything will look better and easier to understand once you do that.
Looks like you're header view is see through. Have you tried to put a background color ?
UIView *view = [[[NSBundle mainBundle] loadNibNamed:#"FutureAppointmentsHeaderView" owner:self options:nil] lastObject];
view.backgroundColor = [UIColor whiteColor];
try to change your tableViewType.
UITableViewStyleGroup can let your header scroll when you scroll the tableView
This is the default feature of tableview, and is not the bug. Just verify the background color of headerview in xib. It seems to have clear color, set it to any other color and you could find the difference. Code seems fine.
You are adding a header for the section with
- tableView:viewForHeaderInSection:
. Section headers stay on top of the table view and will cover the cell(s) underneath it. If you would add another section to your table view there would be two identical header views for each section, because you are returning the view without checking the section index.
If you want a floating header use tableHeaderView.
You can assign a view to UITableView's property in viewDidLoad or by dragging a view to the table view in interface builder. See Table Header Views in StoryBoards.

UItable Footer View issue

In my app I need to design the view with header, content view (table view) and footer view which is scrollable. Content view data will change dynamically. So I have used table view.
I have added the Header and footer view in the table view header and footer. (My goal is to scroll the header, content and footer view so the I have added my custom header and footer view in UItable view header and footer.)
My design:
It's working as per the design if the table view contains some data. Issue is, if table view doesn't contains any data (row count simply 0). The header view is display in the middle of the view (Table view frame shrink automatically in this case). Even I tried to handle the table view frame based on the data source count. But I can't.
How can I fix this issue?
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
You can use this code to customize footer height of UItableview
by putting valid if else
In a UItableView,The footer always appears at the end of section .But here you don't have any existance of cells and you want to show the footer at the bottom of the page .
Solution:
Take one cell before getting data and set the height for that cell
BOOL hasData;//Initialise in ViewDidLoad and set value according to data
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (!hasdata) {
return 1;
}else{
return dataArray.count;
}
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (!hasData) {
cellHeight =
self.view.frame.size.height -
(tableViewHeaderHeight+tableViewFooterHeight);
}else{
//Set row height
}
}
and then after getting data for the tableview again reload that .
Hope this help you.Try this .It worked for me.
Table view sections have a header view and a footer view. Between those are the cells for that particular section. It sounds like you want to simulate the existence of cells that don't really exist.
Perhaps what you really want is a standard UIView as your header and footer views with a UITableView in between. The UITableView won't change size based on its content and will scroll when independently of the header and footer views.

Custom UITableView Footer doesn't lock in the bottom as the table view is scrolled

I have checked all these
UITableView, make footer stay at bottom of screen?
tableFooterView property doesn't fix the footer at the bottom of the table view
iOS - viewForFooterInSection sticking to bottom of UITableView
Proper way to implement a footer in UITableView
similar questions but unfortunately my problem hasn't resolved.
I have to implement a custom header and footer views with buttons inside. I have created separate UIView's subclasses with .nib files. In my view controller, I'm calling these methods to register nibs for header and footer view.
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
CustomTableHeaderView *view = [CustomTableHeaderView header];
view.delegate = self; //setting delegate to receive callbacks as the buttons inside the view are pressed
return view;
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
CustomTableFooterView *view = [CustomTableFooterView footer];
view.delegate = self;
return view;
}
Where as the class method in the custom views registers a .nib file and returns the view. However the implementation is;
+ (CustomTableHeaderView*)header
{
return [[[NSBundle mainBundle]loadNibNamed:#"CustomTableHeaderView" owner:nil options:nil]objectAtIndex:0];
}
Similar implementation for footer.
The problem is that the footer view doesn't lock at the bottom when the table view scrolls. i-e, when there are more rows to fit inside the view, the footer view hides and is revealed when all the rows are scrolled down till the end. I want to lock the footer view at the bottom of the view no matter how much rows are there to scroll.
The header view has been implemented perfectly by this implementation as it is locked at the top while the rows are being scrolled, however the footer view is scrolled with the rows.
I have also tried self.tableview.tablefooterview property but it didn't help either.
Unfortunately thats not how table section footers work. In order to accomplish an anchored view at the bottom you will need to add it as a subview to your UIView manually.
If you add it as a subview to your UITableView you will need to keep it anchored by changing its frame in scrollViewDidSroll:. If you add it as a subview to the UIView containing your UITableView you can just place it statically at the bottom. In either case you probably want to adjust the contentInset of the table view with an inset at the bottom so that you can scroll your content up above the anchored footer.

How to add footer view for UITableView in IB when working with storyboards?

Playing around with prototype cells and I need to add footer view at the bottom of table view, so a user could see this footer view when he would scroll to the bottom of the table view. So, created demo project with one screen, table view and two prototype cells. Looking for a way how to drag and drop some view below the table using Interface Builder. The problem is it looks like view is put outside table view content, so I see the footer but I can't scroll to it (only a small part of footer view is seen at the bottom of the table view).
I know this should work because already saw a working implementation but cannot figure out what magic setting or code line I need to add.
Here are the methods:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section {
return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat rowHeight = 10;
switch (indexPath.row) {
case 0: {
rowHeight = 376;
break;
}
case 1: {
rowHeight = 105;
break;
}
}
return rowHeight;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.row) {
case 0: {
Cell1 *cell1 = [tableView dequeueReusableCellWithIdentifier:#"Cell1" forIndexPath:indexPath];
return cell1;
break;
}
case 1: {
Cell2 *cell2 = [tableView dequeueReusableCellWithIdentifier:#"Cell2" forIndexPath:indexPath];
return cell2;
break;
}
}
return nil;
}
Just making 2 row table with two different height cells. Cell1 and Cell2 classes are empty subclasses of UITableViewCell.
Here's how table view and cells look in interface builder:
Here's initial view after launch:
Here's what I see if I scroll to the bottom:
The footer is there, but outside table view (scroll content). As you can see, table view by default reserves some 44px space at the bottom for footer. But if I set footer height in tableView:heightForFooterInSection: then blank spaces appear.
Also, tried to drag and move this view up to view hierarchy in IB, so the view would become a header view. In that case, the view is shown at the top as header view, but then the second cell is shown only partially when scrolling to the bottom. It looks like table calculates how much space it needs to show prototype dynamic cells (have set "Dynamic Prototypes" for the table view). And if you add extra footer or header view to the interface builder then there's less space for the cells (if view is added as header) or the footer is not shown.
UPDATE. If I add this method then blank spaces appear:
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 50;
}
Here's what I get in that case (blank spaces below cell2):
UPDATE2 Footer is shown correctly if I disable autolayout.
I think you have missed the following line
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return HEIGHT_OF_YOUR_FOOTER_VIEW;
}
Edit
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView *footer=[[UIView alloc] initWithFrame:CGRectMake(0,0,320.0,50.0)];
footer.layer.backgroundColor = [UIColor orangeColor].CGColor;
return footer;
}
Updated
Check this documentation
At last found the solution. Have noticed footer is shown correctly when autolayout is disabled, so started to look at constraints. So, have added leading space to container, trailing space, bottom space and top space to container constraints to table view using Ctrl + Drag. However IB showed red warning about missing Y position constraint. So, after choosing "Add missing constraints" from IB suggestion panel, IB added another system constraint but the footer was still not show correctly. It appears IB was unable to add correct top space and bottom space to container system constraints, and even to fix that. So I'v got 5 system constraints as a result of that:
Adding system constraints using Ctrl + Drag from table view to containing view have worked in previous demos for me. However, this time IB was unable to add correct top space and bottom space to container, and so top space vertical constraint had a value of -568. Tried setting to 0 but it didn't worked. Tried ten times to delete all constraints and add them again. The same result.
So, I deleted all these vertical (bottom and space) constraints and then selected "Add missing constraints". And bingo! IB added correct vertical constraints and the footer view was shown correctly. Here's how correct constraints should look like. However, I still don't understand why IB was unable to add correct constraints when I was doing Ctrl + Drag from table view to container view.

Resources