Hey Everyone,
I am trying to create a UITableViewController where one of the functions is to find out how many of each "section" is in my core data. I'm using the NS predicate to set a predicate for my entity as shown below in my countSection method. My sections are A, B, C, D, E and it's only return 1 for A but I have a lot more of each and the other sections are returning 0. I'm noticing if I change my last entry to B then it returns 1 for B. Is there a reason why it's only looking at the last entry instead of the entire database?
Also, when I try to addObject into my NSMutableArray sectionCountNumber, nothing is being added because when I display the "temp", it's showing (null).
At the end of the loop, I'm getting this error: -[NSCFString stringValue]: unrecognized selector sent to instance 0xb298.
Here's the complete LessonTableView.h code:
Here's my complete LessonTableView.m file:
I am trying to create a UITableViewController where one of the functions is to find out how many of each "section" is in my core data. I'm using the NS predicate to set a predicate for my entity as shown below in my countSection method. My sections are A, B, C, D, E and it's only return 1 for A but I have a lot more of each and the other sections are returning 0. I'm noticing if I change my last entry to B then it returns 1 for B. Is there a reason why it's only looking at the last entry instead of the entire database?
Also, when I try to addObject into my NSMutableArray sectionCountNumber, nothing is being added because when I display the "temp", it's showing (null).
At the end of the loop, I'm getting this error: -[NSCFString stringValue]: unrecognized selector sent to instance 0xb298.
Code:
- (void)countSection {
for (int i = 0; i<[lessonSection count]; i++) {
NSFetchRequest *request =[[NSFetchRequest alloc] init];
if (self.managedObjectContext_ == nil) {
self.managedObjectContext_ = [(OntarioG1TestAppDelegate *)[[UIApplication sharedApplication] delegate]managedObjectContext];
}
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Quiz"
inManagedObjectContext:managedObjectContext_];
[request setEntity:entity];
NSString *sectionName = [lessonSection objectAtIndex:i];
NSLog(@"%@", sectionName);
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"section==%@", sectionName];
[request setPredicate:predicate];
NSError *error;
// the sections are:
sectionCountNumber = [managedObjectContext_ countForFetchRequest:request error:&error];
NSLog(@"%i", sectionCountNumber);
[sectionCount addObject:[NSNumber numberWithInteger:sectionCountNumber]];
NSString *temp = [sectionCount objectAtIndex:i];
NSLog(@"%@", temp);
[request release];
}
}
Here's the complete LessonTableView.h code:
Code:
@protocol LessonTableViewDelegate;
@interface LessonTableView : UITableViewController {
id <LessonTableViewDelegate> delegate;
IBOutlet UIImageView *image;
IBOutlet UILabel *question;
IBOutlet UILabel *answerA;
IBOutlet UILabel *answerB;
IBOutlet UILabel *answerC;
IBOutlet UILabel *answerD;
IBOutlet UIButton *showAnswer;
IBOutlet UIButton *next;
NSInteger qid;
NSInteger counter;
NSInteger sectionCountNumber;
NSManagedObjectContext *managedObjectContext_;
NSMutableArray *lessons;
NSMutableArray *lessonSection;
NSMutableArray *sectionCount;
//Quiz *currentLesson;
}
@property (nonatomic, assign) id <LessonTableViewDelegate> delegate;
@property (nonatomic, retain) UIImageView *image;
@property (nonatomic, retain) UILabel *question, *answerA, *answerB, *answerC, *answerD;
@property (nonatomic, retain) UIButton *showerAnswer, *next;
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext_;
@property (nonatomic, retain) NSMutableArray *lessons, *lessonSection;
@property (nonatomic, retain) NSMutableArray *sectionCount;
@property (nonatomic, assign) NSInteger qid;
@property (nonatomic, assign) NSInteger counter;
@property (nonatomic, assign) NSInteger sectionCountNumber;
-(void)countSection;
@end
@protocol LessonTableViewDelegate
- (void)lessonTableViewDidFinish:(LessonTableView *)lesson;
@end
Here's my complete LessonTableView.m file:
Code:
@implementation LessonTableView
@synthesize delegate;
@synthesize image;
@synthesize question, answerA, answerB, answerC, answerD;
@synthesize showerAnswer, next;
@synthesize lessons, lessonSection;
@synthesize sectionCount;
@synthesize managedObjectContext_;
@synthesize qid, counter, sectionCountNumber;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
lessonSection = [[NSMutableArray alloc] initWithObjects:@"A",@"B",@"C",@"D", @"E", nil];
}
- (void)countSection {
for (int i = 0; i<[lessonSection count]; i++) {
NSFetchRequest *request =[[NSFetchRequest alloc] init];
if (self.managedObjectContext_ == nil) {
self.managedObjectContext_ = [(OntarioG1TestAppDelegate *)[[UIApplication sharedApplication] delegate]managedObjectContext];
}
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Quiz"
inManagedObjectContext:managedObjectContext_];
[request setEntity:entity];
NSString *sectionName = [lessonSection objectAtIndex:i];
NSLog(@"%@", sectionName);
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"section==%@", sectionName];
[request setPredicate:predicate];
NSError *error;
// the sections are:
sectionCountNumber = [managedObjectContext_ countForFetchRequest:request error:&error];
NSLog(@"%i", sectionCountNumber);
[sectionCount addObject:[NSNumber numberWithInteger:sectionCountNumber]];
NSString *temp = [sectionCount objectAtIndex:i];
NSLog(@"%@", temp);
[request release];
}
}
/*
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return [lessonSection count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [[sectionCount objectAtIndex:section] intValue];
}
- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section {
NSString *temp = [[lessonSection objectAtIndex:section] stringValue];
NSLog(@"%@", temp);
return [[lessonSection objectAtIndex:section] stringValue];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
return cell;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
*/
}
#pragma mark -
#pragma mark Memory management
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Relinquish ownership any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
}
- (void)dealloc {
[lessons release];
[image release];
[question release];
[answerA release];
[answerB release];
[answerC release];
[answerD release];
[managedObjectContext_ release];
[lessonSection release];
[sectionCount release];
[super dealloc];
}
@end