我正在尝试制作一个显示日历的应用程序,一旦点击日历上的一天(每一天都是带有点击手势的 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;
}
任何帮助将不胜感激。