Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide a section in UITableView?

There are some section in the table that does not contain any data and would like to hide that section.

How to do this?

like image 416
Teo Choong Ping Avatar asked Jun 30 '09 00:06

Teo Choong Ping


People also ask

How do I delete a cell in UITableView?

So, to remove a cell from a table view you first remove it from your data source, then you call deleteRows(at:) on your table view, providing it with an array of index paths that should be zapped. You can create index paths yourself, you just need a section and row number.

What is Section in UITableView?

UITableView with sections allows us to separate the list into different categories, so the list looks more organized and readable. We can customize sections as per our need, but in this tutorial, we are covering the basic UITableview with sections. Here's is the video if you prefer video over text. Let Create An App.


2 Answers

Actually, you can "hide" a section. If you want to use a similar behaviour to the built-in contacts app, where sections are hidden but still listed in the index on the right you can do the following:

Implement the UITableViewDataSource protocol:

  • Return all section names (even hidden ones) in - the sectionIndexTitlesForTableView method.

  • For each empty section, return nil from the titleForHeaderInSection method.

  • For each empty section return 0 for the numberOfRowsInSection method.

I find this works better than deleting sections, because the user has consistent index navigation.

like image 111
TJez Avatar answered Oct 07 '22 01:10

TJez


You can't "hide" a section as such, but you can "delete" it from the table view using the deleteSections:withRowAnimation: method. This will remove it from the view, with an optional animation, without affecting your backing data. (You should, however, update the data anyway so that the section doesn't reappear.)

More info: UITableView class reference

like image 33
Tim Avatar answered Oct 07 '22 02:10

Tim