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.
Related
By default, the grouped table view has a reserved vertical margin for every section. I don't want the header view but footer view for table view, so how to drop the margin easily?
Override the following method of tableview's delegate to drop the header view's margin directly.
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 0.01; // Removed the section header margin. (couldn't be 0 here, weird)
}
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.
I have a Storyboard which contains a UITableViewController. The table view is static and contains two sections. The table view was setup entirely in IB (including the header and footer text of the table view sections) - I have not implemented any table view delegate methods in my view controller.
If I attempt to get a reference to the UITableViewHeaderFooterView for a given section it always returns nil.
UITableViewHeaderFooterView* header =[self.tableView headerViewForSection:0];
When I run the app I can see the header and footer text that I set in IB so I know those views are there. I just can't figure out how to access them programmatically.
Any help would be much appreciated,
CS
Actually you'll need to specify the table footer view, and pass it to the table view using its delegate.
For example, suppose you have defined a IBOutlet UIView named tableFooterView.
Then in the following delegate method, you can pass your tableFooterView to your table view:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section; // custom view for footer. will be adjusted to default or specified footer height
{
return self.tableFooterView;
}
Also you'll need to implement the following method to set the height of the table footer view:
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return self.tableFooterView.frame.size.height;
}
In my application I would like to hide the table cells in uitableview , and I don't want to hide the header of the tableview in iPhone. On that table view header I would like to add a button, so if anyone know this how to hide the cells in a tableview without hiding the header in tableview in iphone.
If you want to hide all cells in a section, just set the number of rows in that section to 0!
Use table Dalegate method put height of that row 0.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath