-1

我有一个名为数组的自定义对象数组,它存储了一堆核心数据。我使用这个数组来填充我的 UITableView 并创建单元格。然后我尝试在打开 UISearch 时使用相同的数组,但此时数组为空,我怎么能保留数组中的对象?

编辑

好的,这是我的一些代码

我的 .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
4

2 回答 2

0

如果我正确理解您的问题,要保留您的数组,请在 .h 文件中声明该数组,如下所示: @property (retain) NSMutableArray *yourArrayName;

然后,在你的 .m 文件中合成它,如下所示: @synthesize yourArrayName;

于 2012-04-11T03:03:29.653 回答
0

我尝试使用 Retain 方法作为 Oral b 的回答,但它让我无处可去,我对 Objective c 有点陌生,这就是我遇到问题的原因。

我的课程数组是在一个接口中声明的,但我无法使用它获得与我为搜索创建的新数组相同的结果。所以在 ViewDidLoad 我这样做

coursesForSearch = [[NSMutableArray alloc] init];

然后将对象添加到我的另一个数组中,我只是添加了这个数组并将对象添加到其中。

现在我可以像这样从数组中获取对象

[[coursesForSearch objectAtIndex:500] objectAtIndex:0]);
于 2012-04-12T16:44:30.847 回答