Skip to content

Instantly share code, notes, and snippets.

@jungchris
Last active September 17, 2015 17:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jungchris/178ea7b93b999b279e5d to your computer and use it in GitHub Desktop.
Save jungchris/178ea7b93b999b279e5d to your computer and use it in GitHub Desktop.
Filter Method For Tracks
// NOTES: sessionType property from JSON file 'sessions.json' compared to 'tracksString' from 'tracks.json'
//
//
@property (nonatomic, strong) NSString *selectedTrack;
@property (nonatomic, strong) NSArray *filteredArray;
@property (nonatomic, strong) NSArray *sortedArray;
@end
@implementation TracksTableVC
- (void)viewDidLoad {
//
[super viewDidLoad];
// observer for updating view on Parse sessions update
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selectorForTracksUpdate:) name:@"UpdateTracks" object:nil];
// table cells (re-use 'DatesViewCell' as 'TracksCell'
NSString *cellIdentifier = @"TracksCell";
[self.tableView registerNib:[UINib nibWithNibName:@"TracksViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:cellIdentifier];
// header for add banner
NSString *headerIdentifier = @"Header";
[self.tableView registerNib:[UINib nibWithNibName:@"SessionsHeaderCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:headerIdentifier];
// allow for skipping making a selection
UIBarButtonItem *skipButton = [[UIBarButtonItem alloc] initWithTitle:@"See All" style:UIBarButtonItemStyleDone target:self action:@selector(selectorForSkipButton)];
self.navigationItem.rightBarButtonItem = skipButton;
// set up array filtered by date
// self.selectedDate is set by the previous view controller when a date is selected
if (self.selectedDate) {
self.filteredArray = [SharedConferenceObject filterSessionsByDate:self.selectedDate];
// create sorted from filtered array
[self createdSortedArrayOfTracks];
}
}
// this creates the sorted array
// create array sorted by name
- (void)createdSortedArrayOfTracks {
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"sessionTitle" ascending:YES];
NSArray *descriptors = [NSArray arrayWithObject:sortDescriptor];
self.sortedArray = [self.filteredArray sortedArrayUsingDescriptors:descriptors];
}
// display the tracks
// list all the items in array
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellIdentifier = @"TracksCell";
TracksViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if (!cell) {
cell = [[TracksViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
// this code greys out items that don't exist for a selected date
// use filtered sessions by date (changed from self.filteredArray -> self.sortedArray)
if (self.sortedArray) {
// check each track for a session with the selected date
BOOL isTrackFound = NO;
// check each session
for (int j = 0; j < [self.sortedArray count]; j++) {
//
SessionsModel *session = self.sortedArray[j];
NSString *trackName = session.sessionType;
// NSString *trackName = ((SessionsModel*)sessionsByDateArray[j]).sessionType;
//
if ([trackName isEqual:SharedConferenceObject.tracksArray[indexPath.row]]) {
// contains object
isTrackFound = TRUE;
}
}
// set cell greyed out or full view
if (isTrackFound) {
cell.labelTitle.alpha = 1.0f;
cell.imageIcon.alpha = 1.0f;
} else {
cell.labelTitle.alpha = 0.2f;
cell.imageIcon.alpha = 0.2f;
}
}
//
cell.labelTitle.text = SharedConferenceObject.tracksArray[indexPath.row];
UIImage *imageThumbnail = [UIImage imageNamed:@"info.png"];
cell.imageIcon.image = imageThumbnail;
return cell;
}
// -------------------------------------------------------------------------------------------------
// Code below this line is part of ConferenceModel.m
// this code is called by viewDidLoad above
// this method is an instance method of the Confererence object and has access to the sessionsArray created from sessions.json
// this returns a filtered array for a selected date based on the contents of sessionsArray
- (NSArray*)filterSessionsByDate:(NSDate*)date {
NSMutableArray *filteredArray = [[NSMutableArray alloc] init];
for (int i = 0; i < [self.sessionsArray count]; i++) {
// iOS 8 + use [NSCalendar isDate:inSameDayAsDate:]
NSDate *startDate = [CCJTextEngine convertISO8601ToNSDate:[self.sessionsArray[i] sessionStart]];
if ([self isDate:startDate sameDayAsDate:date]) {
// add to array if dates match
[filteredArray addObject:self.sessionsArray[i]];
} else {
// NSLog(@"filtered date no match");
}
}
return [filteredArray copy];
}
// filter array by track
// this public CLASS method is included in the Confererence.m
// track data is obtained from tracks.json. It is a string, so a match is done on that.
+ (NSArray*)filterArray:(NSArray*)inputArray byTrack:(NSString*)track {
NSMutableArray *filteredArray = [[NSMutableArray alloc] init];
for (int i = 0; i < [inputArray count]; i++) {
// iOS 8 + use [NSCalendar isDate:inSameDayAsDate:]
if ([[inputArray[i] sessionType] isEqual:track]) {
// add to arryay if dates match
[filteredArray addObject:inputArray[i]];
} else {
// NSLog(@"NO track match!");
}
}
return [filteredArray copy];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment