I have an array of custom objects called array which stores a bunch of core data. I use this array to populate my UITableView and to create the cells. I then try to use the same array when UISearch is opened but at this point the array is empty, how could I retain the objects in the array?
EDIT
Ok, here is some of my code
my .h file
@interface SelectCourses : UIViewController <DataHandlerDelegate, UITableViewDataSource, UITableViewDelegate, UITableViewDataSource> {
DataHandler *dataHandler;
NSMutableArray *courses;
IBOutlet UITableView *table;
IBOutlet UISearchBar *searchFilter;
//NSMutableArray *filteredCourses;
BOOL isFiltered;
}
@property (retain) NSMutableArray* filteredCourses;
Then my .m file, I have left out several functions which I believe are irrelevant
@implementation SelectCourses
@synthesize Delegate;
@synthesize filteredCourses;
...
...
- (void) dataHandlerHasLoadedCourseData:(NSMutableArray *)courseData {
courses = courseData;
[table reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(isFiltered){
return filteredCourses.count;
}
if (courses) {
NSLog(@"Count: %i", [courses count]);
if (courses.count == 1) {
return 0;
}
return [courses count];
}
else
return 0;
}
...
...
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchFiltert {
NSLog(@"searchBarSearchButtonClicked");//More debugging
if(searchFilter.text.length == 0)
{
isFiltered = FALSE;
}
else
{
NSLog(@"%@", [NSString stringWithFormat:searchFilter.text]);
isFiltered = true;
for (Course *course in courses)
{
NSRange codeRange = [course.Code rangeOfString:searchFilter.text options:NSCaseInsensitiveSearch];
NSRange nameRange = [course.Name rangeOfString:searchFilter.text options:NSCaseInsensitiveSearch];
if(codeRange.location != NSNotFound || nameRange.location != NSNotFound)
{
[filteredCourses addObject:course];
}
}
}
[table reloadData];
}
I still get the following error when running the script, I simply assumed it was due to the array being empty
2012-04-11 20:54:23.067 scheduleTable[45885:fb03] -[__NSArrayM Code]: unrecognized selector sent to instance 0x897b360
2012-04-11 20:54:23.068 scheduleTable[45885:fb03] *** WebKit discarded an uncaught exception in the webView:shouldInsertText:replacingDOMRange:givenAction: delegate: <NSInvalidArgumentException> -[__NSArrayM Code]: unrecognized selector sent to instance 0x897b360
My course.h and .m looks like this
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@class Post;
@interface Course : NSManagedObject
@property (nonatomic, retain) NSString *Code;
@property (nonatomic, retain) NSString *Name;
@end
and
#import "Course.h"
#import "Post.h"
@implementation Course
@synthesize Code;
@synthesize Name;
@end
I tried using the retain method as Oral b answered but it got me nowhere, I am a bit of a newbie to Objective c which is why I am having the issues.
My courses array was declared in an interface and I was not able to achieve the same result with it as I did with the new array I created just for the search. So in ViewDidLoad I do this
Then where objects were added to my other array I simply added this array and also added the objects to it.
Now I can get objects from the array like so