1

我有一个带有很多标签的视图,每个标签对应于工作日和一种任务。我想从核心数据中加载信息并使用该信息来设置每个标签的文本。

#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

但它只在日期是当前日期(顺便说一下星期天)时才注册任务。任何人都可以想出一种更有效的方法来设置每个标签的文本,而不是为每个标签设置一个插座,或者循环浏览所有视图的子视图并检查它们的标签吗?

非常感谢任何帮助。

4

1 回答 1

0

您的谓词现在说“日期为今天或更晚且日期为今天或更早的对象”。唯一满足该要求的对象是带有今天日期的对象。

对于您的其他问题,您可以查看 IBOutletCollection。它们用于类似对象的集合(在您的情况下为 UILabel)。它基本上是 IB 对象的 NSArray,您可以对其进行循环并轻松设置它们的值。

于 2012-11-04T22:41:33.423 回答