App crashes when reloading tableView with new data - iphone

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.

Related

UITableViewController is not updating content is I apply manual sorting

using a UITableViewController, where I am displaying some products which are sorted based on make year. Whenever user taps in a row (each row represents a single product) a new view gets displayed to edit the same product.
So after save if the user has changed the make year then it should display in main table view at appropriate place (i.e. sorted again accordingly).
To accomplish this I am calling sort() method in.... here sort method returns NSMutableArray type of object.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Sort the records based on number of days.
sortedProducts = [DTOConverter sort:unsortedProducts];
return [sortedProducts count];
}
But the problem is that in table view I am still having the old values in that particular row until I bounce the entire view then it gets populated with new updated values.
Same thing happens in case of a new product gets added.
Please correct me where I am doing wrong, I am sure that the issue might be related to indexPath, but i do not know how to tackle it. Please help.Thanks.
To update tableview data call
[tableView reloadData];
You have to refresh the table or the individual row with one of the following methods:
reloadData
reloadRowsAtIndexPaths:withRowAnimation
reloadSections:withRowAnimation
reloadSectionIndexTitles
See the documentation on each method: UITableView Class Reference
Note: if you are only updating one row, do not call [tableView reloadData]. It is not necessary to reload all of the data in the table.
You may have to set up a delegate in the new view you are loading from your tableview and then set up your tableviewcontroller to implement the delegate method of that new viewcontroller. Then call reload data from that delegate method Here is an example of a delegate: Delegate example

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];

Refreshing/releasing 2nd tableview from previous query in drill down tableview

I have a drill down tableview that is using sqlite.
The first one is a tableview where i load some categories using a SQL query.
When i click a row it's using the following query to show the appropriate records for the category:
#"SELECT * FROM table WHERE category IS ('%#')", sharedSingleton.category];
This works.
When i click the back button on tableview 2 and return to the category tableview and then touch another category tableview2 still shows the rows for the category i selected the first time.
I'm new to this stuff but i searched a long time and tried releasing objects or set value's to nil but is doesn't seem to help.
Am i in the right direction, how can i refresh or release tableview2?
Thanks in advance.
Yes you are in the right direction.
When selecting a row table 1 you might be calling a method which loads the second table view and assigning the data to be loaded to the corresponding table view,Which is fetched and filled when delegate/ data source methods of table view is called.
You might have created tableview as a different view controller class, which is initialized and data is reloaded for the first time. But even though you are fetching the second set of data its not getting reloaded automatically even after you are assigning it.
After assigning the data you have fetched into the obect which distributes it to tableview Reload the table data once you assign it to. you could call the one line of code to do this
[tableview reloadData];
Regards,
Jackson Sunny Rodrigues

Deleting all objects from a section in a Core Data backed UITableView doesn't trigger the selection to delete?

I'm having a problem managing the sections in my Core Data backed UITableView, using an NSFetchedResultsController.
My table view can have 2 sections, the first is a static section and is displayed conditionally. The second section is being populated with the fetched results controller. I'm using the following to determine how many sections to display. If I delete all of the fetched objects in the second section, I would expect that section to be deleted as well.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView
{
NSInteger numberOfSections = 0;
if (self.locationEnabled) numberOfSections++;
if ([[[self.fetchedResultsController sections] lastObject] numberOfObjects]) numberOfSections++;
return numberOfSections;
}
The problem with this approach is that this datasource method is informing the table that it should be displaying 1 section because there are no fetched objects remaining, but removing the last object never triggered a deletion of the section through the fetched results manager delegate methods.
How do I trigger a section deletion to avoid receiving UIKit inconsistency errors?
Edit: I should point out that I have sectionNameKeyPath set to nil since the results don't share anything in common to section by.

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

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.