objective-c programmatically change table view from standard to grouped? - iphone

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!

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.

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.

Related

iphone: In group section tableview how do you disable section in search result?

I am using searchDisplayController and it would be searching for the names and also the sections. i would like to know how to show the results of only names and not including the section. Assume the codes are the similar as from apples doc. I have at least 2000 names in there when viewing the tableview itself.
See my answer to this question:
UISearchBar Search table row with text, subtext and image
What you show in a results table is completely up to you. It needn't even have anything to do with the table you're searching! (But of course it usually does, as otherwise you'd confuse the user.) You simply form the data that populates the results table; what data that is, is your call.
So, if you don't want to include any section titles, then when the table inquiring of your data source / delegate is the results table, don't include any section titles! It's your code, it's your table, do whatever you want. You are the one implementing tableView:titleForHeaderInSection: to return titles; if you don't want titles, return nil instead. Of course, if the data source for the real table is the same object as the data source for the results table, then tableView:titleForHeaderInSection: will have to examine the incoming tableView parameter to see whether it is the real table or the results table, and make its choice of what to return based on that.
I would like to a bit more of your problem as it is not much clear. I don't actually get this line:
i would like to know how to show the results of only names and not including the section
For searching, it is best to search in a dictionary/array and show the result in tableview by [tableView reloadData].

UITableView - Adding additional rows

I have created a UITableView in an app, using the basis of a tutorial from the following link;
http://www.theappcodeblog.com/?p=353
I have customised the style to my needs and it works perfectly. However, if i wanted to add additional rows to the table I come across a problem. I have my three NSMutableArrays which contain category, subCategory and categoryDetails. I have added an additional entry to each of the existing 6 to form a new row.
I assumed that would be ok, as the numberOfRowsInSection code is set to return [category count];
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [category count];
}
I assumed that by adding an entry for each array, adding an image it would check the category count and auto add a new row. However, what it actually does it repeat the first array and then stack the new one on top of it overlapping.
Any ideas?
It would seem that its something to do with the cellForRowAtIndexPath. Without actually seeing the code i'd say that you are adding new labels pertaining to the 7th row to the already created 6th row. And hence the overlapping.
Are you calling the reload data method of the table view when your data changes?
[tableView reloadData];

How can I return the count of objects for an FRC fetch?

I am trying to use the count of objects from my FRC fetch so that I can create an "Add" cell after all of my fetched objects have been listed in a UITableView. Is there a method for returning the number of objects fetched? I can only find a method that returns the count of sections for a given FRC.
Simplistically, you You want a count of fetchedObjects. So:
[[myFRC fetchedObjects] count];
The problem that you're going to have with adding an additional cell, is that every time the table ask for the count of sections and rows, you're going to have add 1 to the count so the table knows to add the extra row.
Bjarne Mogstad is correct. It's quicker and cleaner to put the add element in a header or footer for the table itself. That way, the table directly and cleanly represents the data structure without having to constantly adjust for the phantom row.
The easiest way to do this is by setting the tableFooterView property of your table view. The footer view will be displayed below the last table view cell.

App crashes when reloading tableView with new data

I've got a view containing a segmentedControl and a tableView. The tableView is populated depending on the segmentedControl item that is selected (in this case Food and wine). The data for the tableView is generated from coreData.
It works fine when starting up the application which any of the segmentedControl items selected (food or wine) and is displaying the right data. But as soon as I try to select the other item the app crashes saying "Program received signal: “EXC_BAD_ACCESS”.". Sadly the debugger does not give me any legible information to know where the exception happens, so I inserted breakpoints and it seems to happen in
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
MenuSubsections* menuSubsection = [self.menuSubsections objectAtIndex:section];
if (![menuSubsection menuItems]) {
return 0;
}
return [[menuSubsection menuItems] count];
}
in the last line.
I inserted a NSLog for menuSubsection and it says, among other stuff that is right,
menuItems = "<relationship fault: 0x8133540 'menuItems'>";
But it also gives the same message when starting up and working fine...
Any idea?
Thanks,
Miguel
It looks like you've in effect two logical tables being displayed in a single table. When you choose one segmented control or the other you're swapping out the entities, Food or Wine, displayed by the table.
This is fine but each entity will have its own data structure that defines it's own version of the table and you have to make sure you return the proper number of sections and rows for the currently selected entity. It is unlikely that there is exactly the same number of Food entities as Wine entities
It looks like your MenuSubsections class only returns the count of rows for a section for one of the entities. When you switch to the other entity it keeps returning the row count for the first entity which causes the table to access objects for rows that don't exist which causes the crash.
Your numberOfRowsInSection: method needs an additional layer of logic such that it checks the value of the segmented control and then returns the right MenuSubsections for either Food or Wine.

UITableView - implementing index

I want to implement an index that is being displayed at the right side of a table. I've found out how to display it.
How can I scroll to a certain position in my table, after a certain index item was pressed?
In the index I am not displaying all the alphabet, but in the following manner:
#define ALPHA_ARRAY= #"A●D●G●J●M●P●S●V●Z"
So when the user presses between A and D I want to go to items starting from A and so on. .
How can I implement it?
I want to make something similar to the index available in the contacts application on iPhone.
You need to implement this method in your table view data source:
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
The table view gives you the index of the section the user touched, along with its title (in this case, one of the letters you specified). You need to return the index of the section. If there are exactly as many sections as there are indexes, all you have to do is return index. Otherwise, you'll need to figure out the index of the section the table view should scroll to and return that instead.