Differentiate groups of rows visually by adding header and footer views to your table view’s sections.
Use header and footer views as visual markers for the beginning and end of sections. Header and footer views are optional, and you can customize them as much or as little as you want.
To create a basic header or footer with a text label, override the
table method of your table’s data source object. The table view creates a standard header or footer for you and inserts it into the table at the specified location.
Customize the Header and Footer Views
Custom header and footer views give the sections of your table a unique appearance. With custom headers and footers, you specify the views you want and position them anywhere within the allotted space. You can also provide different header or footer views for different sections of your table.
To create custom header or footer views:
UITableobjects to define the appearance of your headers and footers.
View Header Footer View
UITableobjects with your table view.
View Header Footer View
Always use a
UITable object for your headers and footers. That view supports the same reuse model that cells employ, allowing you to recycle views instead of creating them every time. After you register your header or footer view, use the table view’s
dequeue method to request instances of that view. If recycled header or footer views are available, the table view returns those first, before creating new ones.
You can use a
UITable object as is and add views to its
content property, or you can subclass and add your views. Position your subviews inside the content view by using a stack view or Auto Layout constraints. To change the background behind your content, modify the
background property of the header-footer view. The following example code shows a custom header view that positions the image and label views at creation time.
Register your header view as part of configuring your table view.
In your delegate’s
table method, create and configure your custom view. The following example code dequeues the registered custom header and configures its title and image properties.
The following image shows the resulting headers.
Change the Height of Headers and Footers
A table view tracks the height of section headers and footers separately from the views that represent them.
UITable provides default sizes for both items. If all of your headers (or all of your footers) are the same height, specify that height using the table view’s
section properties, or use the default values provided by the table view.
If your header or footer heights are not all the same, or can change dynamically, provide the heights using the
table methods of your delegate object. When you implement these methods, you must provide values for every header and footer in your table. The table view asks for the heights of visible headers and footers only. As the user scrolls, the table view asks you to provide the height for each one as it appears, including when it moves offscreen and then back onscreen.