1

Under some circumstances I want to hide a section of an UITableView.

I’ve solved this by returning 0 rows for the section and nil for the section title.

After that the section is empty / not visible, but there’s a bigger gap than usual between the remaining sections.

enter image description here

I’ve tried to set the heightForHeader and heightForFooter to 0.0 but the space remains bigger.

How can I avoid this extra space between the sections?

ixany
  • 5,433
  • 9
  • 41
  • 65

2 Answers2

1

I’ve found a solution in this thread.

Long story short: You have to set self.tableView.sectionHeaderHeight = 0.0 as well as self.tableView.sectionFooterHeight = 0.0 i.e. in viewDidLoad.

For whatever reason the heightForHeader and heightForFooter delegate methods are ignored.

ixany
  • 5,433
  • 9
  • 41
  • 65
0

Returning 0 rows for the section and nil for the section title does not mean that the section is hidden. It only means that the data of the section is cleared thats why the area equivalent to the section size still appears .

Solution :

You should put condition in numberOfSections & cellForRowAt functions that allow or disallow that section (for example with a Bool variable ) and when you want to hide it just turn the Bool false and reloadData of the UITableView.

Mina Gerges
  • 319
  • 3
  • 15
  • Reloading the whole TableView just to hide a single section isn’t a good approach in my opinion. Using my current implementation, I can explicitly use `reloadSections` to refresh just the affected section and also animate it smoothly in the same breath. – ixany Oct 18 '18 at 16:10
  • 1
    You are right I agree with you.I think the below link can help you . https://stackoverflow.com/questions/1061208/how-to-hide-a-section-in-uitableview – Mina Gerges Oct 18 '18 at 16:20
  • Thank You @Mina Hanna – your answer led me into the right direction! – ixany Oct 19 '18 at 14:12