views:

82

answers:

2

Hi,

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.

A: 

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.

RickiG
A: 

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

Michael Kessler
Hi, thanks for the post. Returning 0 in numberOfRowsInSection leaves a noticeable blank area where the section used to be.
Vivas
Then you could maintain a mutable array of arrays as the data source for your table. The top array will represent the sections and the inner one will represent the cells under the sections. This way your top array will contain the exact amount of sections (according to the data - if there is no data for one of the sections then there will be no element for that section in the top array).
Michael Kessler