How can I detect the day of the week and the time, and then display data in a tableview based on the current day and time?
I have a list of data that is a schedule. Each item has a time that i开发者_开发问答t takes place in. I want to detect the current day and time and then display what items are available during that time. All I really know is how to get todays date and time and that I need to create a method to look in my data at what is currently "playing". Any suggestions?
By assuming that your schedule items are stored in an NSArray
called scheduleItems
and that these items have a date
property, you could filter them with a predicate:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"date == %@",
[NSDate date]];
NSArray *todaysItems = [scheduleItems filteredArrayUsingPredicate:predicate];
The problem is that this will give you every item where its date is exactly now. You probably want to compare the date in a range:
NSDate *today = ...;
NSDate *tomorrow = ...;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"date BETWEEN %@",
[NSArray arrayWithObjects:today, tomorrow,nil]];
NSArray *todaysItems = [scheduleItems fileteredArrayUsingPredicate:predicate];
Note: this is not tested.
精彩评论