Obtaining the UITableViewHeaderFooterView of a static UITableView - ios

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;
}

Related

iCarousel View in a table cell view does not scroll at all

I put the iCarousel View in a table View and try to scroll the iCarousel, but the problem is it's not scrollable at all.
I can see iCarousel in the cell like following:
and I put iCarousel datasource and delegate in the tableView Controller, create a customer function to set the iCarousel datasource and delegate.
In the CustomTableViewCell I just define the setting delegate function:
- (void) setICarouselDataSourceDelegate: (id<iCarouselDelegate,iCarouselDataSource>)dataSourceDelegate {
self.carousel.dataSource = dataSourceDelegate;
self.carousel.delegate = dataSourceDelegate;
[self.carousel reloadData];
}
and in the MainTableView, I call the DataSourceDelegate in the
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
First of all iCarousel is not any standarad View ... you should put link of the control that you're using
Second it seems like iCarousel is uses UIScrollView and UITableView is also derived from UIScrollView It's never a good idea to put a scrollview inside another scrollview.
This might be the reason why your table view is not scrolling.... BTW are you talking about horizontal scrollign or vertical scrolling ?
Currently I am using the iCarousel in Table View cell and it scrolls easily.
Just add standard view to cell in Storyboard.
In IB mark the class of that view to iCarousel.
Furthermore !ENABLE! user interaction on that view. (Maybe because of that you can't scroll the content inside the cell)
Implement separate datasource & delegate for every cell. (It is more clear which data are loading in the cell and also the cell itself can handle the data. You just pass the data to cell and it handles it accordingly.) - But it depends on you if you want to let one class to handle two separate control datasources & delegates :-)

How to set my view as my TableViewheader?

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.

Is it possible to have a fixed uitableview Header while using sections?

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.

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.

Table view first cell always same

well my issue is:
I want to have table view but I want to keep first cell always first , and never let it scroll
but other cells should scroll.
can someone please suggest me how to do so ?
If your table have only one section than create custom view like cell and add this view in
table delegate method
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
like
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
return <Your View>
}
but it works if and only if one section in given in table.
The above answer with the static cell in the header works.
You might want to create another tableview just above the present one, with just static cells in it. Just below this table view, keep the present one with (number of all cells - number of static cells) without any offset. It looks as just one table view. This way, you can have more than one static cell with all the properties of a Tableview cell.

Resources