1

我正在尝试制作一个显示日历的应用程序,一旦点击日历上的一天(每一天都是带有点击手势的 UIView),那一天的所有日历约会都应该显示在 UITableView 中。我已经完成了这项工作,但是在点击发生和数据实际填充到 UITableView 之间存在很大的延迟。这是我的代码:

EKEventStore *store = [[EKEventStore alloc] init];

//Access Granted to Calendar by user
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {

// Create the start date components
NSDateFormatter *startFormatter = [[NSDateFormatter alloc]init];
[startFormatter setDateFormat:@"MM/dd/yyyy hh:mm a"];

NSString *monthNumberString = [NSString stringWithFormat:@"%i", month];

NSString *startDateString = [[[[[monthNumberString stringByAppendingString:@"/"] stringByAppendingString:dLabel.text] stringByAppendingString:@"/"] stringByAppendingString:yearString] stringByAppendingString:@" 12:01 am"];

NSDate *start = [startFormatter dateFromString:startDateString];

NSLog(@"Start Date: %@", startDateString);

// Create the end date components
NSDateFormatter *endFormatter = [[NSDateFormatter alloc]init];
[endFormatter setDateFormat:@"MM/dd/yyyy hh:mm a"];

NSString *endDateString = [[[[[monthNumberString stringByAppendingString:@"/"] stringByAppendingString:dLabel.text] stringByAppendingString:@"/"] stringByAppendingString:yearString] stringByAppendingString:@" 11:59 pm"];

NSDate *end = [endFormatter dateFromString:endDateString];
NSLog(@"End Date: %@", endDateString);

// Create the predicate from the event store's instance method
NSPredicate *predicate = [store predicateForEventsWithStartDate:start
                                                            endDate:end
                                                          calendars:nil];


// Fetch all events that match the predicate
events = [store eventsMatchingPredicate:predicate];

//Sort the array
events = [events sortedArrayUsingSelector:@selector(compareStartDateWithEvent:)];

int eventCount = [events count];

NSLog(@"%i", eventCount);

for (int i=0; i<eventCount; i++) {
    EKEvent *theEvent = [events objectAtIndex:i];    
    NSLog (@"Element %i = %@", i, theEvent.title);
}

UITableView *dayTableView = [[UITableView alloc] initWithFrame:CGRectMake(360, 0, 300, 550)
                                                                 style:UITableViewStylePlain];
dayTableView.backgroundColor = lightBlueColor;
dayTableView.separatorColor = [UIColor clearColor]; 
dayTableView.delegate = self;
dayTableView.dataSource = self;

[super addSubview:dayTableView];

}];

UITableview 委托功能:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSLog (@"I made a section!");
    return 1;    //count of section


}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    NSLog (@"I made %i rows!", [events count]);
    return [events count];
}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *c = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"mycell"];

    EKEvent *theEvent = [events objectAtIndex:indexPath.row];

    c.textLabel.text = theEvent.title;

    NSLog (@"Cell %i = %@", indexPath.row, theEvent.title);



    //c.textLabel.text = @"Calendar Event Goes Here";
    c.textLabel.textColor = [UIColor whiteColor];

     //NSLog (@"I made a cell!");

    return c;    


}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{




    return 35;
}

任何帮助将不胜感激。

4

2 回答 2

0

我找到了答案。问题在这里:[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL grant, NSError *error) {

在 iOS6 和 iOS5 上访问事件存储的方式是不同的。这是一个显示正确方法的链接:

http://fostah.com/ios/2012/09/28/ios6-event-edit.html

于 2013-06-07T20:49:13.753 回答
0

如果不进行分析,就很难说出代码的瓶颈是什么。但是,根据以前的经验,我会说这是看起来像的线条

NSString *startDateString = [[[[[monthNumberString stringByAppendingString:@"/"] stringByAppendingString:dLabel.text] stringByAppendingString:@"/"] stringByAppendingString:yearString] stringByAppendingString:@" 12:01 am"];

至少如果它每秒被称为 +20000x(或其他东西)。首先,把它写成这样会更方便

NSString *startDateString = [NSString stringWithFormat:@"%@/%@/%@ 12:01 am", monthNumberString, dLabel.text, yearString];

但我担心这不会大大加快你的程序。我建议回到纯 C 代码并使用 egsprintf代替。您可以在此处查找它的语法以及一些使用示例。

于 2013-06-03T21:50:38.560 回答