Hi,
I'm trying to make some change to Apple's basic template that comes when we chooose Navigation Based Application with Core Data for iPhone using iOS sdk 4.1. The template has one table and Edit/+ button on the left and right side respectively at the table's navigation bar.
*What I've been working on is to add UIToolBar at the bottom with 'sort' button on it.* (it just sort the table cells whenever I touch it. ascending-decending based on its timeStamp)
Just managed insert toolbar at the bottom. Final UI is below.
using `[self.navigationController.view addSubview:toolbar];` at viewDidLoad. [1]
Sorting works fine; when I press 'sort' button on the ToolBar after populate some cells using '+' button, it sorts the table cells using NSSortDescriptor with 'timeStamp'.
Below is 'sort' button's method;
and fetchedResultsController methods is below;
**The problem is when I press either Edit or + button after the sorting. The app just freeze & gives me the error message below;**
Basically I just just set my fetched controller to nil, and allow the ViewController to regenerate it based on the sorting keyword. (I'm planning using 2 to 3 kinds of sortKeyword later...which get decided by user at runtime)
I also added [self.tableView reloadData] to controllerDidChangeContent method.
good news is
1. launch the app
2. populate table using + button
3. press sort button
4. and press + again
5. it freeze.
6. kill the app
but If I launch the app again, the cell is increased by one..so the app freezes after + button done its job.
I think the buttons between ToolBar and NavigationBar are not communicating well regarding on fetchedResultsController.
My guess is..it's something to do with 'fetchedResultsController Cache' but I can't spot where should I change and how....maybe I'm wrong maybe it'd be barking at a wrong tree...I dunno..
any help would be really appreciated!
[1] www.iphonesdkarticles.com/2008/09/navigation-controller-uitoolbar.html
I'm trying to make some change to Apple's basic template that comes when we chooose Navigation Based Application with Core Data for iPhone using iOS sdk 4.1. The template has one table and Edit/+ button on the left and right side respectively at the table's navigation bar.
*What I've been working on is to add UIToolBar at the bottom with 'sort' button on it.* (it just sort the table cells whenever I touch it. ascending-decending based on its timeStamp)
Just managed insert toolbar at the bottom. Final UI is below.

using `[self.navigationController.view addSubview:toolbar];` at viewDidLoad. [1]
Sorting works fine; when I press 'sort' button on the ToolBar after populate some cells using '+' button, it sorts the table cells using NSSortDescriptor with 'timeStamp'.
Below is 'sort' button's method;
Code:
- (IBAction) toolbarNameButtonPressed:(id) sender{
orderKeyword = @"timeStamp";
if (bool4Order == YES) {
bool4Order = NO;
}
else {
bool4Order = YES;
}
fetchedResultsController_ = nil;
[self.tableView reloadData];
}
and fetchedResultsController methods is below;
Code:
- (NSFetchedResultsController *)fetchedResultsController {
if (fetchedResultsController_ != nil) {
return fetchedResultsController_;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setFetchBatchSize:20];
//** Edit the sort key as appropriate.**
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:sortKeyword ascending:bool4Order];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
[aFetchedResultsController release];
[fetchRequest release];
[sortDescriptor release];
[sortDescriptors release];
NSError *error = nil;
if (![fetchedResultsController_ performFetch:&error]) {
abort();
}
return fetchedResultsController_;
}
**The problem is when I press either Edit or + button after the sorting. The app just freeze & gives me the error message below;**
> *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1262.60.3/UITableView.m:920
2010-11-03 19:08:35.274 SortTest02[2229:207] Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (3) must be equal to the number of rows contained in that section before the update (3), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted). with userInfo (null)
Basically I just just set my fetched controller to nil, and allow the ViewController to regenerate it based on the sorting keyword. (I'm planning using 2 to 3 kinds of sortKeyword later...which get decided by user at runtime)
I also added [self.tableView reloadData] to controllerDidChangeContent method.
good news is
1. launch the app
2. populate table using + button
3. press sort button
4. and press + again
5. it freeze.
6. kill the app
but If I launch the app again, the cell is increased by one..so the app freezes after + button done its job.
I think the buttons between ToolBar and NavigationBar are not communicating well regarding on fetchedResultsController.
My guess is..it's something to do with 'fetchedResultsController Cache' but I can't spot where should I change and how....maybe I'm wrong maybe it'd be barking at a wrong tree...I dunno..
any help would be really appreciated!
[1] www.iphonesdkarticles.com/2008/09/navigation-controller-uitoolbar.html