CraigH
CraigH

Reputation: 1417

Grouped table view empty section

I've read every answer available for this question that I could find, including this answer here on SO: link text

The best known example of what I am trying to accomplish would probably look like the detail screen of the Contacts app. Some contacts have a phone number and email, others have multiple phone numbers but no email etc. My data is coming in from a remote server and some people records may have a phone number, others may have 3 email addresses but no phone, others may have 2 phone numbers and 2 email addresses etc.

Any suggestions on how to handle this, again using Contacts as an example (grouped table view with variable number of sections and variable rows per section). In the link I posted above, one of the answers that makes sense it to simply use deleteSections:withRowAnimation: but I am unclear how I would go about implementing that in this scenario since I am not using commitEditingStyle, I just want to 'remove' sections on the fly that are empty.

Thank you for your time.

Upvotes: 0

Views: 506

Answers (2)

Michael Kessler
Michael Kessler

Reputation: 14235

Just return 0 from - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section for the sections that are empty.

Upvotes: 0

RickiG
RickiG

Reputation: 11390

Why do you want to build empty cells, just to remove them again? Write a class that formats your retrieved data into a data object that your UITableViewController understands, say, a SectionDataObject.

First off define some sections, e.g. an email section, a phone number section etc. Let your SectionDataObject hold an array for each of these section categories, an emailsArray, a phoneNumbersArray etc.

Now in all the delegate methods you just reference the different arrays of the SectionDataObject. It is no longer an issue if a user has got 15 emails and no phone number, the emailsArray will return count 15 and the phoneNumberArray will return count 0.

Upvotes: 1

Related Questions