我有一个包含大量标签的视图,每个标签对应一个工作日和一种任务。我想从核心数据加载信息并使用该信息来设置每个标签的文本。
#import "WeekViewController.h"
@implementation WeekViewController
- (void) viewDidLoad {
gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
[gregorian setFirstWeekday:2];
mondayMeetings = 0;
mondayPhoneCalls = 0;
mondayToDos = 0;
tuesdayMeetings = 0;
tuesdayPhoneCalls = 0;
tuesdayToDos = 0;
wednesdayMeetings = 0;
wednesdayPhoneCalls = 0;
wednesdayToDos = 0;
thursdayMeetings = 0;
thursdayPhoneCalls = 0;
thursdayToDos = 0;
fridayMeetings = 0;
fridayPhoneCalls = 0;
fridayToDos = 0;
saturdayMeetings = 0;
saturdayPhoneCalls = 0;
saturdayToDos = 0;
sundayMeetings = 0;
sundayPhoneCalls = 0;
sundayToDos = 0;
[self loadInfo:[NSDate date]];
}
- (void) loadInfo:(NSDate *)date {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(Date >= %@) AND (Date <= %@)", [self dateByMovingToBeginningOfWeek:self.selectedDate], [self dateByMovingToEndOfWeek:self.selectedDate]];
request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Task" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
[request setPredicate:predicate];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Date" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptors release];
[sortDescriptor release];
NSError *error = nil;
mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults == nil) {
}
for (int i = 0; i < [mutableFetchResults count]; i++) {
Task *aTask = (Task *)[mutableFetchResults objectAtIndex:i];
NSDateComponents *comps = [gregorian components:NSWeekdayCalendarUnit fromDate:aTask.Date];
int weekday = [comps weekday];
if (weekday == 1 && [aTask.Type isEqualToString:@"PhoneCall"]) {
mondayPhoneCalls ++;
}
}
NSLog(@"%d", mondayPhoneCalls);
for (UILabel *go in [self.view subviews]) {
if (go.tag == 1) {
go.text = [NSString stringWithFormat:@"%i", mondayPhoneCalls];
}
}
}
- (NSDate *) dateByMovingToEndOfWeek:(NSDate *)date {
unsigned int flags = NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekdayCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *parts = [gregorian components:flags fromDate:date];
[parts setWeekday:7];
[parts setHour:23];
[parts setMinute:59];
[parts setSecond:59];
return [gregorian dateFromComponents:parts];
}
- (NSDate *) dateByMovingToBeginningOfWeek:(NSDate *)date {
unsigned int flags = NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekdayCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *parts = [gregorian components:flags fromDate:date];
[parts setWeekday:0];
[parts setHour:0];
[parts setMinute:0];
[parts setSecond:0];
return [gregorian dateFromComponents:parts];
}
@end
但它只注册一个任务,如果它的日期是当前日期(顺便说一句)。也可以有人想到一种更有效的方法来设置每个标签的文本,而不是每个标签都有一个插座,或者循环浏览所有视图的子视图并检查它们的标签?
任何帮助都非常感激。
答案 0 :(得分:0)
您的谓词现在说“日期为今天或更晚的对象,其日期是今天或更早的日期”。满足该要求的唯一对象是具有今天日期的对象。
对于您的其他问题,您可以查看IBOutletCollection。它们用于类似对象的集合(在您的情况下为UILabel)。它基本上是IB对象的NSArray,您可以循环并轻松设置它们的值。