Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we hide the tableHeaderView and tableFooterView?

I have two buttons which i add it in one in table-footer and other one in table-header,i know how to hide the headerview of the table using this codetable.tableHeaderView.hidden = YES; but the problem is there is still space in the top portion of the table.That space is equal to the header-view size,but the view is hidden .It still has the space.How can we disable the table-header by removing this space.I hope you genius developers understand my question.Please help me. Thanks in advance.

like image 419
ICoder Avatar asked Nov 10 '11 12:11

ICoder


People also ask

How to add a header and footer view to a Tableview?

As I mentioned before there are two ways to add a header and footer view, but there are also two ways using just code. The first way will use set the tableViewHeader property on the tableview and the second way will use UITableViewDelegate methods.

How to hide a table header with JavaScript?

There are two approaches that can help to hide a table header with the help of JavaScript. They are discussed below: Approach 1: Select the header using a CSS selector and modify the style property such that the value of the display property is set to none. This will hide the selected table header element from the page.

How to create self-sizing Table View headers using UIView?

Let's start by creating a UIView subclass with single label which will act as a HeaderView. We expect this UILabel to be multiline to demonstrate our ability to support self-sizing table view headers. Next, inside your viewDidLoad method add following piece of code to instantiate Component and assign it as a header view

What is the difference between header and footer?

The footer is basically the same as the header but there can be a catch if you only want a footer view. To only have a footer view, you will need to add a prototype cell, that will allow you to add a footer view below the cell.


2 Answers

Instead of hiding the header view you should do,

tableView.tableHeaderView = nil 

And later if you want to show it then just assign it again,

tableView.tableHeaderView = tableHeaderView; 

In Swift:

class myTableViewController: UITableViewController {      @IBOutlet var tableHeaderView: UIView!      private func toggleHeaderView() {         if tableView.tableHeaderView == nil {             tableView.tableHeaderView = tableHeaderView          } else {              tableView.tableHeaderView = nil          }     }  } 

on your Storyboard, simply drag a UIView in to the table view. It will "magically" become the table view header (if you do another one, it will become the table view footer). HOWEVER you must click on that header view, and drag the referencing outlet to the table view controller, and link it to "tableHeaderView" ... that part is not "magic".

Note that because of the "!" in the declaration, you have to remember to drag the link on Storyboard or you'll get a runtime error during testing, so that's a good thing.

like image 115
EmptyStack Avatar answered Sep 20 '22 18:09

EmptyStack


Try:

tableView.tableHeaderView?.removeFromSuperview() tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: CGFloat.leastNonzeroMagnitude)) tableView.layoutIfNeeded() 

Set an UIView with height as CGFloat.leastNonzeroMagnitude instead of '0'. This will remove the blank space appearing at the top after removing the tableViewHeader. This worked for me.

like image 34
Alfa Avatar answered Sep 21 '22 18:09

Alfa