我正在尝试创建一个显示日历的应用程序,并且只要日历上的一天(每天都是带有轻击手势的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;
}
非常感谢任何帮助。
答案 0 :(得分: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代码并使用例如而是sprintf
。您可以查找它的语法here以及一些使用示例。
答案 1 :(得分:0)
我找到了答案。问题出在这里: [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL grant,NSError * error){
在iOS6和iOS5上访问事件存储的方式不同。这是一个显示正确方法的链接: