假设我有不同日期的NSMutableArray。日期以UITableView表示。如果我单击UITableViewCell,如何从NSMutableArray获取同一天的所有日期?
假设我在周一插入了3个日期,周二插入了1个日期,周三插入了4个日期,依此类推。如果我按下代表星期一的1/3日期,我如何才能获得所有3个星期一的日期而忽略其他日期?
如果可能,我如何对它们进行排序??因此,如果我在8,9,10小时(都在周一)中保存,并按下任何小时(也包括分钟),它们将被排序为8-9-10,就像它们实际发生的那样……
Tnx。
发布于 2013-05-11 21:47:21
NSDate和company的日期计算有点复杂,但一旦你掌握了它的诀窍,实际上是相当强大的。以下是过滤数组的一种方法:
int main (int argc, const char * argv[])
{
@autoreleasepool {
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSMutableArray *dates = [NSMutableArray array];
NSDate *selectedDate = [NSDate date];
for (int i=0; i<70; i++)
[dates addObject:[NSDate dateWithTimeIntervalSinceNow:i * 60*60*24]];
NSInteger selectedDayOfWeek = [calendar components:NSWeekdayCalendarUnit fromDate:selectedDate].weekday;
NSPredicate *filter = [NSPredicate predicateWithBlock:^(id object, NSDictionary *bindings) {
return (BOOL)([calendar components:NSWeekdayCalendarUnit fromDate:object].weekday == selectedDayOfWeek);
}];
NSArray *filteredDates = [dates filteredArrayUsingPredicate:filter];
NSArray *sortedDates = [filteredArray sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"%@", filteredDates);
}
return 0;
}https://stackoverflow.com/questions/16497596
复制相似问题