I'm trying to give space between cells in my tableview for that i written the following code:
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
switch (section)
{
case 0:
return 0.0f;
break;
default:
return 15.0f;
break;
}
return 15.0f;
}
up to now everything is fine, but after scrolling tableview gap is not moving when it reaches to top to table. My screens are:
You should not use header view for this purpose. Just design you cells so that their subviews (labels, buttons, images...) are contained in a view that is smaller than the cell itself and centered horizontally. Make the cell background grey, and the subviews' view container white.
The header stops at the top because you have the table style set as plain. If you change the table style to group it will scroll off the top as you are hoping.
So go to the attribute inspector for the tableView and change to style group.
Related
I have UITableView with two sections. Now, because I wanted additional space for header between 0 and 1st section, I've gave them different heights:
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
switch (section) {
case 0:
return 30;
break;
case 1:
return 60;
default:
return 0;
break;
}
}
Now, this is working pretty good, I have space view and then in last 30px of view I have label. Problem is when that view is scrolled on top, there is 30px spacing between navigationBar and label(that space view that I made).
Is there any way to detect is header of section 1 is scrolled to top and change height for it if it's on top?
Try to change your tableview to group style
tableView.style = UITableViewStyle.Grouped
After I reviewed what I could do, I've removed this space view and added height for footer in section 0.
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];
I've got a UITableView with one section and enough rows that the tableView needs to be scrolled to get to the bottom. I want to add a footer view which will stick to the bottom of the tableView and always be visible, so I have implemented viewForFooterInSection. Here's my code:
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
NSLog(#"Get footer view");
if (tableView == [self tableView]) {
return [self footerRowRightView];
}
else if (tableView == [self fixedColumnTableView]) {
return [self footerRowLeftView];
}
return nil;
}
The problem I am having is that the footer view only shows after the tableView has been scrolled, but I want it to be visible from the outset (i.e. always floating whether the user scrolls or not).
As soon as the controller appears and the tableView loads its data, I see "Get footer view" in the log, so I know that viewForFooterInSection is being called straight away. What I can't work out is why it doesn't display immediately, and how to get it to do so.
Thanks in advance for any help!
It is probably your height for the footer not being returned correctly.
Check what you return from heightForFooterInSection
What you need is not a tableview's footer.
Simply add the view corresponding to this header in the superview of your tableview and put it at the bottom of it. Then simply reduce the height of the frame of your tableview to fit the remaining space. And it should do it !
You can no use footer view if you want to stick the footer. Or try with grouped tableview.
Quite a few options by the looks of the other answers. Just to add a hacky workaround I have just come up with, I created duplicates of the views I will be using as footers and added them as subviews of my main view, placed exactly over the position of where the real footer views. The views are retained in properties, so that in scrollviewDidScroll I can do the following:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if ([self preScrollFooterLeftView]) {
[[self preScrollFooterLeftView] removeFromSuperview];
}
if ([self preScrollFooterRightView]) {
[[self preScrollFooterRightView] removeFromSuperview];
}
}
This way the footer appears to be displayed immediately. The fake footer is removed as soon as the user scrolls the tableView, revealing the real footer beneath it. If the tableView is scrolled below the last row, the real header sticks to the bottom of the section and bounces back to the bottom of the tableView when the user lets go.
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.
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.