views:

58

answers:

2

Hello all,

The following situation pertains to an iPad app I am working on for my company.

So I have an array of type "Person" that holds the information of a list of people. On the initial page, I have a table that displays their name and picture. When this loads, the results are ungrouped. I was wondering if there was a way to easily group these results with the click of a button based on something like location or business title. The reason they are not grouped when they are loaded is because the powers higher then I deemed it necessary to display the raw list first. Any ideas on doing something like that? Thanks in advance!

+1  A: 

You'll have to remove the UITableView and add another in it's place via [[UITableViewController alloc] initWithStyle:UITableViewStyleGrouped];. You can't change an existing style at runtime.

The other way to do it, is have it grouped from the get-go, but return 1 for - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; if it is "ungrouped" and return all rows for - (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section when it is grouped, do it the normal way.

Matt Williamson
How would you call those methods again once the user clicks the group button?
gabaum10
[myTable reloadData];
Matt Williamson
+1  A: 

You'll want to use "sections" to split the table.

First you need to sort your datasource into the desired groups, and then reflect it in the view.

A dictionary can easily represent the grouped data. Model each group as an array that contains the entries for that group, and then add it to the dictionary with a key that represents the section.

When the data is ungrouped, you can still use the dictionary, but with a single entry, rather than many, and then reflect that in the UI.

That way when you're asked how many sections there are in the table you can return [dictionary count]; and then when you're asked for the number of rows, you can do something like [[dictionary objectForKey@"myKey"] count];

And so on.

When you've reconfigured your datasource you can call [self.tableView reloadData]; to reload the table with the sections.

Jasarien
This is a really good idea. I am going to give it a shot and get back to you. Thanks!
gabaum10
Ok I have the data all separated as desired, now I just need to reconfigure the table. How do you go about changing the table # of sections? Or pretty much any of the table attributes. I have found a bunch of ways to do that when the page loads but nothing to do it at runtime. Any thoughts?
gabaum10
Calling `reloadData` on the table view will instruct it to query the `dataSource` and `delegate` again for the updated information. You need to manage the state in your controller and conditionally change the number that you return in `numberOfSections...` and `numberOfRows...` etc.
Jasarien
Alright, this is where it gets kind of complicated. I am actually building up the table cell by cell based on the content of my person array. There isn't a delegate or datasource or anything like that. Should I try to figure out how to substitute in the grouped dictionary instead of using the data directly?
gabaum10
It would be less complicated if you used the dataSource and delegate. The table view is intended to be used with a data source and delegate. You're just making life harder for yourself by not using them.
Jasarien
Ok, well each cell is dynamically populated with stuff for each person. Does that matter?
gabaum10
Nope, you can populate the cell in the `cellForRowAtIndexPath:` data source method that you will implement.
Jasarien
Ok awesome, So I took this basic idea and played with my parameters a bit. Instead of trying to group a table by the filter, I had the user go one level deeper and select the filter they wanted specifically, and then just pushed a new table out. A lot easier then what I was trying to do. Thanks!
gabaum10