Get a EKEvent from a array iOS - ios

I want to get an EKEvent from an array iOS. But I have one problem. If I put my code lines in the viewDidLoad method, it works.
But if I make it in a button from a table view: (-(IBAction)button:(UiButton *)sender) it gets an EKEvent, but doesn't note the event.
Thats my code:
EKEvent *event =[eventArray objectAtIndex:0]
NSString *string = event.notes
Thats the complete code:
- (void)viewDidLoad {
selectedDate = [NSDate date];
[super viewDidLoad];
EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (granted == NO) {
UIViewController *controller = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"needAccess"];
[self.view addSubview:controller.view];
[self addChildViewController:controller];
}
}];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *start = [calendar dateBySettingHour:0 minute:0 second:0 ofDate:selectedDate options:0];
NSDate *end = [calendar dateBySettingHour:0 minute:0 second:0 ofDate:[selectedDate dateByAddingTimeInterval:60*60*24] options:0];
NSPredicate *predicate = [store predicateForEventsWithStartDate:start endDate:end calendars:nil];
eventArray = [store eventsMatchingPredicate:predicate];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [eventArray count];
}
- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [table dequeueReusableCellWithIdentifier: #"EventCell"];
EKEvent *event = [eventArray objectAtIndex:indexPath.row];
UIButton *headline = (UIButton *)[cell viewWithTag:1];
headline.layer.cornerRadius = headline.frame.size.width / 2;
headline.clipsToBounds = YES;
headline.tag = indexPath.row;
[headline addTarget:self action:#selector(headlineButton:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
-(void)headlineButton:(UIButton*)sender {
EKEvent *event = [eventArray objectAtIndex:0];
NSArray *notes = [event.notes componentsSeparatedByString:#"//"];
NSString *latitude = [notes objectAtIndex:1];
NSString *longitude = [notes objectAtIndex:2];
}

That's probably because neither the cell or the button on the cell have an EKEvent property.
Your cell is loaded from a model. The event data is likely in the model
If your button or cell needs to access event data, you have to decide how the data flows through your app. You also want to have a single source of truth for the event, so no matter what your app's state, you always are certain about details for an event, whether editing or updating or persisting or deleting or inserting an event.
These are questions you may want to tackle, so you have a really good understanding where and how you store these details, and how your cell button should access an event's note.

Related

UITableView reloadData not working when button tapped

I want self.agendaTable to run reloadData when the calendarDidDateSelected runs, but that doesn't seem to happen. As far as I can tell, the table is setup properly, and I have it set to update the contents of self.agendaTableArray in cellForRowAtIndexPath when it reloads. What am I doing wrong?
I'm setting up the table like so:
- (void)viewDidLoad
{
[super viewDidLoad];
// Set up Day Agenda table
CGRect frame = CGRectMake(0,380,self.view.frame.size.width,self.view.frame.size.height);
UITableView *agendaTable = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
agendaTable.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
agendaTable.delegate = self;
agendaTable.dataSource = self;
[agendaTable reloadData];
[self.view addSubview:agendaTable];
/////
self.agendaTableArray = [[NSArray alloc] init];
self.agendaTableArray = #[#"No events today!"];
[self.calendar setMenuMonthsView:self.calendarMenuView];
[self.calendar setContentView:self.calendarContentView];
[self.calendar setDataSource:self];
}
Relevant cellForRowAtIndexPath code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
///...earlier code snipped for brevity
// title of the item
if (self.datePicked == [NSNumber numberWithInt:16]) {
NSLog(#"cellForRowAtIndexPath says self.datePicked is 16");
self.agendaTableArray = #[#"Dinner with Rebekah", #"Meeting with John"];
}
else {
self.agendaTableArray = #[#"No events today!"];
}
cell.textLabel.text = self.agendaTableArray[indexPath.row];
cell.textLabel.font = [UIFont systemFontOfSize:14];
return cell;
}
calling calendarDidDateSelected like so:
- (void)calendarDidDateSelected:(JTCalendar *)calendar date:(NSDate *)date
{
NSLog(#"Date: %#", date);
// NSDateFormatter is used to create a date from a string
// static keyword is used to avoid create a new instance each time calendarDidDateSelected is called
static NSDateFormatter *dateFormatter = nil;
if(!dateFormatter){
dateFormatter = [NSDateFormatter new];
dateFormatter.dateFormat = #"yyyy-MM-dd"; // Read the documentation for dateFormat
}
// If date picked is June 16th
NSDate *juneSixteenth = [dateFormatter dateFromString:#"2015-06-16"];
if([juneSixteenth compare:date] == NSOrderedSame){
self.datePicked = [NSNumber numberWithInt:16];
NSLog(#"self.datePicked: %#", self.datePicked);
}
[self.agendaTable reloadData];
}
Are you properly assigning your agendaTable you created to your 'agendaTable' property in your viewDidLoad?
self.agendaTable = agendaTable;

Inline date picker crashed on second attempt

I have been struggling for this issue for two week now. but by the help of StackOverflow people I have came up with 95% successful implementation..
Here is my problem.. and now I could load my results with the picker cell [First most cell] when the date is selected.
Now I have another issue...when i run my application at first time, it is works as I expected..but when i tap the first most cell to pick a different date , then the application crashed..
Here is the log output...
below is my codes for the implementation
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSDictionary* reqFdate= [ScheduleView getRequestForDate];
if (reqFdate.count == 0) {
NSInteger numberOfRows = [self.persons count];
if ([self datePickerIsShown]){
numberOfRows++;
}
return numberOfRows;
}
else{
return reqFdate.count + 1;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// display in 12HR/24HR (i.e. 11:25PM or 23:25) format according to User Settings
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *currentTime = [dateFormatter stringFromDate:today];
NSDate *date=[dateFormatter dateFromString:currentTime];
if(indexPath.row==0){
VCPerson *person = self.persons[0];
cell = [self createPersonCell:person];
}
else if ([self datePickerIsShown] && (self.datePickerIndexPath.row == 1)){
// VCPerson *person = self.persons[indexPath.row -1];
cell = [self createPickerCell:date];
}
else{
cellForDatePickCell *cell = (cellForDatePickCell*)[self.tableView dequeueReusableCellWithIdentifier:kOtherCellIdentifier];
cell.delegate_Dtepick = self;
return cell;
}
if(indexPath.section!=0 && tapfirstCell) {
UITableViewCell *cell = (UITableViewCell*)[self.tableView dequeueReusableCellWithIdentifier:kOtherCellIdentifier forIndexPath:indexPath];
//cell.delegate_Dtepick = self;
NSDictionary *dictionary = [_dataArray objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:#"data"];
NSString *cellValue = [array objectAtIndex:indexPath.row];
cell.textLabel.text =cellValue;
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.tableView beginUpdates];
if ([self datePickerIsShown] && (self.datePickerIndexPath.row - 1 == indexPath.row)){
[self hideExistingPicker];
// [self.tableView reloadData];
//[self viewDidLoad];
//call the service and take the results
NSString* selecteDate = [ScheduleView getDate];
NSString* prsonID =[LoginView getPersonID];
NSDictionary* parms = [NSDictionary dictionaryWithObjectsAndKeys:prsonID,#"caregiverPersonId",selecteDate,#"selectedDate", nil];
jsonpaser* jp = [[jsonpaser alloc]init];
[jp getWebServiceResponce:#"http://qa.vardle.com/Mobile/WebServices/AppointmentService.asmx/GetAppointments" :parms success:^(NSDictionary *responseObject)
{
requestsF_date = responseObject;
NSLog(#"RESPONSEFORDATE_IN DIDSELECT :%#",requestsF_date);
NSArray* indexpaths = [self getIndexPaths];
NSLog(#"indexPATHS %#",indexpaths);
[self.tableView reloadData];
}];
// cellForDatePickCell *cell = (cellForDatePickCell*)[self.tableView dequeueReusableCellWithIdentifier:kOtherCellIdentifier forIndexPath:indexPath];
// cell.delegate_Dtepick = self;
//tapfirstCell = true;
/*
cellForDatePickCell *cell=(cellForDatePickCell*)[tableView cellForRowAtIndexPath:indexPath];
if(![cell.textLabel.text isEqualToString:#"5/23/14"])
{
return;
}
*/
if (tapfirstCell==false) {
tapfirstCell = true;
}
else{
tapfirstCell = false;
}
}else {
NSIndexPath *newPickerIndexPath = [self calculateIndexPathForNewPicker:indexPath];
if ([self datePickerIsShown]){
[self hideExistingPicker];
}
[self showNewPickerAtIndex:newPickerIndexPath];
self.datePickerIndexPath = [NSIndexPath indexPathForRow:newPickerIndexPath.row + 1 inSection:0];
}
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
[self.tableView endUpdates];
}
please someone tell me where is the issue..why app is crashed when i try to pick a date second time..
please help
i have fix my issue... i didnt deleted the rows before i start to show the date picker again..
here is the full code
here what i did was : i use Inline Datepicker with the table view [date picker will show by selecting the first cell of the table view].according to the date selected from the picker, i am displaying my custom cells below the first cell.[because the first cell always there to pick a date from a date picker].
if someone want to do the same thing which i did i think my code will help you
thank you

UITableView Population Slowness

I'm trying to make an app with a calendar being displayed and as soon as a day on the calendar (each day is a UIView with a tap gesture) is tapped, all calendar appointments for that day should be displayed in a UITableView. I've got this working, but there's a big lag between when the tap occurs and when the data is actually populated into the UITableView. Here's my code:
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 Delegate Functions:
- (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;
}
Any help would be greatly appreciated.
Without profiling it is difficult to say what the bottleneck of your code is. However, based on previous experience I would say that it is the lines that looks like
NSString *startDateString = [[[[[monthNumberString stringByAppendingString:#"/"] stringByAppendingString:dLabel.text] stringByAppendingString:#"/"] stringByAppendingString:yearString] stringByAppendingString:#" 12:01 am"];
At least if it is called +20000x (or something) a second. First of all it would be more convienient to write this as
NSString *startDateString = [NSString stringWithFormat:#"%#/%#/%# 12:01 am", monthNumberString, dLabel.text, yearString];
But I fear this would not speed up your program much. I would recommend going back to plain C code and use e.g. sprintf instead. You can look up the syntax of it here as well as some examples of use.
I found the answer. The problem is here:
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
the way that the event store is accessed is different on iOS6 and iOS5. Here's a link that shows the correct way of doing it:
http://fostah.com/ios/2012/09/28/ios6-event-edit.html

Using NSSortDescriptor to sort UITableview title with setDateFomat:#"dd.MMMM-EEEE

I have a title in UITableView with different dates with the formate of dd.MMMM-EEEE. How can I sort them according to time from old to new? Should I use NSSortDescriptor?
Here is my code:
#synthesize managedObjectContext, pictureListData;
#synthesize scroll;
-(IBAction)scrolldown:(id)sender{
NSCalendar *cal = [NSCalendar currentCalendar];
NSDate *today = [NSDate date];
NSInteger index = NSNotFound;
if ([pictureListData containsObject:today]) {
index = [pictureListData indexOfObject:today];
}
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
[self.tableView scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionNone
animated:YES];
}
-(void)viewDidLoad{
NSSortDescriptor * sortDesc = [[NSSortDescriptor alloc] initWithKey:#"self" ascending:YES];
[pictureListData sortUsingDescriptors:[NSArray arrayWithObject:sortDesc]];
[self.tableView reloadData];
}
- (void)viewWillAppear:(BOOL)animated
{
// Repopulate the array with new table data
[self readDataForTable];
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"dd.MMMM - EEEE"];
NSDate *today = [NSDate date];
[pictureListData enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
Pictures *picture = (Pictures *)obj;
NSDate *date = [dateFormatter dateFromString:picture.title];
NSDateComponents *components = [cal components:NSDayCalendarUnit
fromDate:date
toDate:today
options:0];
if ([components day]==0) {
*stop = TRUE;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:idx inSection:0];
[self.tableView scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionTop
animated:YES];
}
}];
}
// When the view reappears, read new data for table
// Grab data for table - this will be used whenever the list appears or reappears after an add/edit
- (void)readDataForTable
{
// Grab the data
pictureListData = [CoreDataHelper getObjectsForEntity:#"Pictures" withSortKey:#"title" andSortAscending:YES andContext:managedObjectContext];
// Force table refresh
[self.tableView reloadData];
}
#pragma mark - Actions
// Button to log out of app (dismiss the modal view!)
#pragma mark - Segue methods
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get a reference to our detail view
PictureListDetail *pld = (PictureListDetail *)[segue destinationViewController];
// Pass the managed object context to the destination view controller
pld.managedObjectContext = managedObjectContext;
// If we are editing a picture we need to pass some stuff, so check the segue title first
if ([[segue identifier] isEqualToString:#"EditPicture"])
{
// Get the row we selected to view
NSInteger selectedIndex = [[self.tableView indexPathForSelectedRow] row];
// Pass the picture object from the table that we want to view
pld.currentPicture = [pictureListData objectAtIndex:selectedIndex];
}
}
#pragma mark - Table view data source
// Return the number of sections in the table
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
// Return the number of rows in the section (the amount of items in our array)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [pictureListData count];
}
// Create / reuse a table cell and configure it for display
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Get the core data object we need to use to populate this table cell
Pictures *currentCell = [pictureListData objectAtIndex:indexPath.row];
// Fill in the cell contents
cell.textLabel.text = [currentCell title];
cell.detailTextLabel.text = [currentCell desc];
// If a picture exists then use it
if ([currentCell smallPicture])
{
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
cell.imageView.image = [UIImage imageWithData:[currentCell smallPicture]];
}
return cell;
}
// Swipe to delete has been used. Remove the table item
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// Get a reference to the table item in our data array
Pictures *itemToDelete = [self.pictureListData objectAtIndex:indexPath.row];
// Delete the item in Core Data
[self.managedObjectContext deleteObject:itemToDelete];
// Remove the item from our array
[pictureListData removeObjectAtIndex:indexPath.row];
// Commit the deletion in core data
NSError *error;
if (![self.managedObjectContext save:&error])
NSLog(#"Failed to delete picture item with error: %#", [error domain]);
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
#end
Modify your dataSource initializing method. Regular sortDescriptor is not sufficient as you need to change the dateString to date for comparison.
- (void)readDataForTable
{
// Grab the data
pictureListData = [CoreDataHelper getObjectsForEntity:#"Pictures" withSortKey:#"title" andSortAscending:YES andContext:managedObjectContext];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"dd.MMMM - EEEE"];
[pictureListData sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
Pictures *picture1 = (Pictures *)obj1;
Pictures *picture2 = (Pictures *)obj2;
NSDate *date1 = [dateFormatter dateFromString:picture1.title];
NSDate *date2 = [dateFormatter dateFromString:picture2.title];
return [date1 compare:date2];
}];
// Force table refresh
[self.tableView reloadData];
}

Have JSON data, would like to show with sections in IOS6 app

I have retrieved my events from a shared google calendar i IOS, and are displaying them in a tableview. As for right now, they're simplty shown with the event title as the title, and the eventtime as the subtitle. I'd like to use the date (eventtime) as a section header in stead, so that all events on for instance march 31st where shown in a group, with the time as a "right detail"
My code looks like this:
#interface MasterViewController () {
NSArray *events;
}
#end
#implementation MasterViewController
-(void)viewDidAppear:(BOOL)animated
{
//show loader view
//[HUD showUIBlockingIndicatorWithText:#"Fetching JSON"];
//make HTTP call
NSString* searchCall = [NSString stringWithFormat:#"http://www.google.com/calendar/feeds/kao1d80fd2u5kh7268caop11o4%%40group.calendar.google.com/public/full?alt=json"];
[JSONHTTPClient getJSONFromURLWithString: searchCall
completion:^(NSDictionary *json, JSONModelError *err) {
//got JSON back
NSLog(#"Got JSON from web: %#", json);
if (err) {
[[[UIAlertView alloc] initWithTitle:#"Error"
message:[err localizedDescription]
delegate:nil
cancelButtonTitle:#"Close"
otherButtonTitles: nil] show];
return;
}
//initialize the models
events = [CalendarModel arrayOfModelsFromDictionaries:
json[#"feed"][#"entry"]
];
if (events) NSLog(#"Loaded successfully models");
//show the videos
[self.tableView reloadData];
}];
}
;
#pragma mark - table methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return events.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CalendarModel* event = events[indexPath.row];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ss.SSSzzz";
NSDate *gmtDate = [formatter dateFromString: [[event.time objectAtIndex:0] startTime]];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:#"%#",
event.title
];
cell.detailTextLabel.text = [formatter stringFromDate: gmtDate];
return cell;
}
#end
I have found this code online, but he doesn't get data as JSON, but from an iPhone calendar, i believe.
#interface MasterViewController ()
#property (strong, nonatomic) NSMutableDictionary *sections;
#property (strong, nonatomic) NSArray *sortedDays;
#property (strong, nonatomic) NSDateFormatter *sectionDateFormatter;
#property (strong, nonatomic) NSDateFormatter *cellDateFormatter;
- (NSDate *)dateAtBeginningOfDayForDate:(NSDate *)inputDate;
- (NSDate *)dateByAddingYears:(NSInteger)numberOfYears toDate:(NSDate *)inputDate;
#end
#implementation MasterViewController
#synthesize sections;
#synthesize sortedDays;
#synthesize sectionDateFormatter;
#synthesize cellDateFormatter;
- (void)awakeFromNib
{
[super awakeFromNib];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
NSDate *now = [NSDate date];
NSDate *startDate = [self dateAtBeginningOfDayForDate:now];
NSDate *endDate = [self dateByAddingYears:1 toDate:startDate];
EKEventStore *eventStore = [[EKEventStore alloc] init];
NSPredicate *searchPredicate = [eventStore predicateForEventsWithStartDate:startDate endDate:endDate calendars:nil];
NSArray *events = [eventStore eventsMatchingPredicate:searchPredicate];
self.sections = [NSMutableDictionary dictionary];
for (EKEvent *event in events)
{
// Reduce event start date to date components (year, month, day)
NSDate *dateRepresentingThisDay = [self dateAtBeginningOfDayForDate:event.startDate];
// If we don't yet have an array to hold the events for this day, create one
NSMutableArray *eventsOnThisDay = [self.sections objectForKey:dateRepresentingThisDay];
if (eventsOnThisDay == nil) {
eventsOnThisDay = [NSMutableArray array];
// Use the reduced date as dictionary key to later retrieve the event list this day
[self.sections setObject:eventsOnThisDay forKey:dateRepresentingThisDay];
}
// Add the event to the list for this day
[eventsOnThisDay addObject:event];
}
// Create a sorted list of days
NSArray *unsortedDays = [self.sections allKeys];
self.sortedDays = [unsortedDays sortedArrayUsingSelector:#selector(compare:)];
self.sectionDateFormatter = [[NSDateFormatter alloc] init];
[self.sectionDateFormatter setDateStyle:NSDateFormatterLongStyle];
[self.sectionDateFormatter setTimeStyle:NSDateFormatterNoStyle];
self.cellDateFormatter = [[NSDateFormatter alloc] init];
[self.cellDateFormatter setDateStyle:NSDateFormatterNoStyle];
[self.cellDateFormatter setTimeStyle:NSDateFormatterShortStyle];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#pragma mark - Date Calculations
- (NSDate *)dateAtBeginningOfDayForDate:(NSDate *)inputDate
{
// Use the user's current calendar and time zone
NSCalendar *calendar = [NSCalendar currentCalendar];
NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
[calendar setTimeZone:timeZone];
// Selectively convert the date components (year, month, day) of the input date
NSDateComponents *dateComps = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:inputDate];
// Set the time components manually
[dateComps setHour:0];
[dateComps setMinute:0];
[dateComps setSecond:0];
// Convert back
NSDate *beginningOfDay = [calendar dateFromComponents:dateComps];
return beginningOfDay;
}
- (NSDate *)dateByAddingYears:(NSInteger)numberOfYears toDate:(NSDate *)inputDate
{
// Use the user's current calendar
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setYear:numberOfYears];
NSDate *newDate = [calendar dateByAddingComponents:dateComps toDate:inputDate options:0];
return newDate;
}
#pragma mark - UITableViewDataSource methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.sections count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSDate *dateRepresentingThisDay = [self.sortedDays objectAtIndex:section];
NSArray *eventsOnThisDay = [self.sections objectForKey:dateRepresentingThisDay];
return [eventsOnThisDay count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSDate *dateRepresentingThisDay = [self.sortedDays objectAtIndex:section];
return [self.sectionDateFormatter stringFromDate:dateRepresentingThisDay];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *reuseIdentifier = #"EventTitleCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
NSDate *dateRepresentingThisDay = [self.sortedDays objectAtIndex:indexPath.section];
NSArray *eventsOnThisDay = [self.sections objectForKey:dateRepresentingThisDay];
EKEvent *event = [eventsOnThisDay objectAtIndex:indexPath.row];
cell.textLabel.text = event.title;
if (event.allDay) {
cell.detailTextLabel.text = #"all day";
} else {
cell.detailTextLabel.text = [self.cellDateFormatter stringFromDate:event.startDate];
}
return cell;
}
#end
Is there a way for me to use my json event.startdate as a section header?
Personally, I would ignore the code you posted second there because its not what you are trying to do.
The part of the code where you set you header and subtitle are these two lines:
cell.textLabel.text = [NSString stringWithFormat:#"%#",event.title];
cell.detailTextLabel.text = [formatter stringFromDate: gmtDate];
So if you want the heading to be the date, just place it as the cell.textLabel.text value.
So if you are wanting to do sections.
Make sure the number of sections is greater then 1.
Then you might want to take a look at these table methods. Just depending on what your exact case and customization needs are you can change it accordingly.
(NSString*) tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section{
}
(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section{
}
(UIView*)tableView:(UITableView*)table viewForHeaderInSection:(NsInteger)section{
}
I hope this helps. I normally would have an example I just didn't have the most available time currently, but if need be I can provide one.
Take Care :)

Resources