曜日とタスクの種類に対応するラベルがたくさんあるビューがあります。コア データから情報を読み込み、その情報を使用して各ラベルのテキストを設定したいと考えています。
#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
ただし、日付が現在の日付(ちなみに日曜日)の場合にのみタスクを登録します。また、各ラベルのテキストを設定するより効率的な方法を、それぞれにアウトレットを用意するか、ビューのすべてのサブビューをループしてタグをチェックするよりも考えられますか?
どんな助けでも大歓迎です。