我有一個名爲array的自定義對象數組,它存儲了一堆核心數據。我使用這個數組填充我的UITableView並創建單元格。然後,我嘗試在UISearch打開時使用相同的數組,但此時數組爲空,我如何保留數組中的對象?如何在NSMutableArray中保留對象IOS5
編輯
好吧,這裏是我的一些代碼
我.h文件中
@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;
然後我.m文件,我已經離開了幾個功能,我相信是無關
@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];
}
我仍然得到運行該腳本時,下面的錯誤,我只是認爲這是由於數組爲空
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
我course.h和.M看起來像這樣
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@class Post;
@interface Course : NSManagedObject
@property (nonatomic, retain) NSString *Code;
@property (nonatomic, retain) NSString *Name;
@end
和
#import "Course.h"
#import "Post.h"
@implementation Course
@synthesize Code;
@synthesize Name;
@end
當你把對象變成他們保留一個NSMutableArray你應該張貼一些代碼 – nielsbot 2012-04-11 02:48:42
。 – 2012-04-11 03:30:14