Spike Lee
Spike Lee

Reputation: 411

Filtering data nsarray iphone

i'm trying to filter data and display it into a uitableview but i'm having some problems filtering the data.

I have an NSArray 'courses' that contains the following data :

    Code = "";
    Comment = "None ";
    Core = Core;
    CourseTitle = "Games";
    Module = test;
    TutorEmail = "";
    TutorName = "";
    day = Monday;
    day2 = Tuesday;
    day3 = Wednesday;
    day4 = Thursday;
    day5 = Monday;
    id = 2;

In my application I have an UITableview that displays the nsarray data and also a tabbar enumerated "Monday,Tuesday...'Friday". When the user presses on any of them , i have the following code to filter the data for that perticular day. (which works fine).

NSPredicate* predicate = [NSPredicate predicateWithFormat:@"day== %@ && CourseTitle==%@", day,courseSelected];
rows = [[courseArray filteredArrayUsingPredicate:predicate]retain];
[tableview reloadData];

The problem is, how can i check and display the same row more than once if day2,day3,day4,day5 is equal to the current 'day' that the user selects?. So for example, if i select 'Monday', the UITableView should display the same record twice (because 'day' and 'day5' contains the string 'Monday')?

Upvotes: 1

Views: 823

Answers (2)

sinh99
sinh99

Reputation: 3979

For filtering data from array use NSPredicate like this

NSMutableArray *tempArray=[[NSMutableArray alloc]init];
[tempArray addObject:@"one"];
[tempArray addObject:@"two"];
[tempArray addObject:@"three"];
[tempArray addObject:@"four"];
[tempArray addObject:@"five"];
[tempArray addObject:@"one"];    
//filter data in self array object
NSString *match = @"one";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF == %@", match];
NSArray *results = [tempArray filteredArrayUsingPredicate:predicate];
NSLog(@"search is %@",[results description]);

Upvotes: 1

aLevelOfIndirection
aLevelOfIndirection

Reputation: 3522

You cannot get the desired outcome by using a single predicate on your model array. What you might consider is a CourseDay model object consisting of a [Course, Day] pair.

So the above model object would translate to 5 seperate CourseDay objects:

[[Course, Monday], [Course, Tuesday], [Course Wednesday], [Course, Thursday], [Course, Friday]]

Note that Course in the above array is only one object not five copies. With this array you could apply your filter: "day=%@ && course.CourseTitle==%@" to get the outcome you want.

Upvotes: 1

Related Questions