UITableView reloadData not working when button tapped - ios

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;

Related

Get a EKEvent from a array 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.

Resizing and re-positioning UILabel inside custom UICollectionViewCell

i'm trying to reposition a UILabel inside my custom UICollectionViewCell with nib.
The problem iss , that at the start, only the first item's subviews looks as it should (positioned by code), and all the other items subviews are positioned like the nib.
if i scroll back and forth a few times, it loads all the other items OK as well.
I tried with and without AutoLayout as well as stated in some answers HERE and tried setting / removing Constrains as told here
Loosing my mind, any help will be great!
in my FeedCell i did this:
-(void)layoutSubviews{
[super layoutSubviews];
[self sortViews];
}
- (void) sortViews{
[_fromLable sizeToFit];
[_toLable sizeToFit];
[_fromYearLabel sizeToFit];
[_dashLabel sizeToFit];
[_dashLabel centerHorizontalInParent];
// rotate the year labels
[_fromYearLabel setTransform:CGAffineTransformMakeRotation(M_PI / 2)];
[_toLable setX:[_dashLabel x] + [_dashLabel width] + 15 ];
[_fromYearLabel setX:[_dashLabel x] - [_fromYearLabel width] - 15 ];
}
I tried calling [cell setNeedsDisplay] in cellForItemAtIndexPath and also in willDisplayCell:forItemAtIndexPath
My cell is dequeue in the cellForItemAtIndexPath like this:
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
FeedCell *cell= (FeedCell*)[collectionView dequeueReusableCellWithReuseIdentifier:#"FeedCell" forIndexPath:indexPath];
NSDictionary *feed = [_datasource objectAtIndex:indexPath.row];
if (feed) {
[cell.mediaImage setHidden:NO];
NSString* desc = [feed objectForKey:#"desc"];
if(desc){
cell.descriptionTextview.text = desc;
[cell.descriptionTextview adjustFontSizeToFillItsContentWithMaxFontSize:80 andMinValue:8 WithFont:fMyriadProRegular];
[cell.descriptionTextview setHidden:NO];
}
else
[cell.descriptionTextview setHidden:YES];
if ([feed objectForKey:#"title"]) {
cell.titleLabel.text = [feed objectForKey:#"title"];
}
if ([feed objectForKey:#"place"]) {
cell.placeLabel.text = [feed objectForKey:#"place"];
}
if ([feed objectForKey:#"location"]) {
[cell.locationLable setHidden:NO];
cell.locationLable.text = [feed objectForKey:#"location"];
}else{
[cell.locationLable setHidden:YES];
}
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:#"YYYY-MM-dd'T'HH:mm:ss"];
NSDate *dateFromString = [[NSDate alloc] init];
NSInteger yearStart = 0;
NSInteger yearend = 0;
NSInteger totalYears = 0;
if ([feed objectForKey:#"startTime"]) {
NSString* startTime = [feed objectForKey:#"startTime"];
dateFromString = [dateFormatter dateFromString:startTime];
cell.fromLable.text = [dateFromString monthStringWithLength:3 capital:YES];
yearStart = [dateFromString year];
}
if ([feed objectForKey:#"endTime"]) {
NSString* endTime = [feed objectForKey:#"endTime"];
dateFromString = [dateFormatter dateFromString:endTime];
cell.toLable.text = [dateFromString monthStringWithLength:3 capital:YES];
yearend = [dateFromString year];
}
if (yearStart == 0 && yearend == 0) {
[cell.totalYears setHidden:YES];
}else if (yearStart > 0 && yearend == 0){
[cell.totalYears setHidden:NO];
totalYears = [[NSDate date] year] - yearStart;
}else{
[cell.totalYears setHidden:NO];
totalYears = yearend - yearStart;
}
cell.fromYearLabel.text = stringWithFormat(#"%zd", yearStart);
cell.totalYears.text = stringWithFormat(#"(%zd YEARS)", totalYears);
[cell.mediaImage setImageWithURL:[NSURL URLWithString:getImageUrlwithIdAndSize([[[_datasource objectAtIndex:indexPath.row] objectForKey:#"media"] objectAtIndex:0], 1)]];
}
cell.backgroundColor= [UIColor whiteColor];
[cell setNeedsDisplay];
return cell;
}
Try the following code:
(void)awakeFromNib
{
[super awakeFromNib];
self.contentView.frame = self.bounds;
self.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}

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

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