Next to a label showing the value of a slider i want to show the date that corresponds to a start date + the number of days in slider value units.
When i move the slider the app crashes in the Simulator. As far as i could find out that happens when i try to create a new NSDate field and my startDate object being "out of scope".
Reading a lot regarding retaining and releasing NSDate documentation had not helped me until now, because the documentation is so abstract that it seems to me that I'm not able to find the relevant part(s) in the documentation. I hope to understand more by example.
So what's wrong with the following code (and from where in the documentation should a beginner have learned that) ?
#import <UIKit/UIKit.h>
#interface SliderDateViewController : UIViewController {
UILabel *dateLabel;
UILabel *sliderValueLabel;
UISlider *slider;
NSDate *startDate;
}
#property (nonatomic, retain) IBOutlet UILabel *dateLabel;
#property (nonatomic, retain) IBOutlet UILabel *sliderValueLabel;
#property (nonatomic, retain) IBOutlet UISlider *slider;
#property (nonatomic, retain) NSDate *startDate;
-(IBAction)sliderValueChanged:(UISlider *)theSlider;
#end
#import "SliderDateViewController.h"
#implementation SliderDateViewController
#synthesize dateLabel;
#synthesize sliderValueLabel;
#synthesize slider;
#synthesize startDate;
- (void)viewDidLoad {
[super viewDidLoad];
dateLabel.text = #"01.01.2010";
sliderValueLabel.text = #"0";
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:#"dd.MM.yyyy"];
self.startDate = [inputFormatter dateFromString:dateLabel.text];
NSLog(#"startDate: ", self.startDate.description);
[inputFormatter release];
}
-(IBAction)sliderValueChanged:(UISlider *)theSlider {
int sliderValueAsInt = (int)(theSlider.value + 0.5f);
NSString *newText = [[NSString alloc] initWithFormat:#"%d", sliderValueAsInt];
sliderValueLabel.text = newText;
[newText release];
// Next line following the comment crashed with
// *** -[CFDate release]: message sent to deallocated instance 0x3b07d10
NSDate *newDate = [startDate addTimeInterval:86400 * sliderValueAsInt];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:#"dd.MM.yyyy"];
dateLabel.text = [outputFormatter stringFromDate:newDate];
[outputFormatter release];
// [newDate release] // <- this was the error
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
}
- (void)dealloc {
[dateLabel release];
[sliderValueLabel release];
[slider release];
[startDate release];
[super dealloc];
}
#end
Firstly, -addTimeInterval: is deprecated. Use -dateByAddingTimeInterval: instead.
NSDate *newDate = [startDate addTimeInterval:86400 * sliderValueAsInt];
[startDate release];
startDate = newDate;
[newDate release];
Since -addTimeInterval: is not an +alloc/-copy/-create method, the newDate is owned by no one and you should not -release it. To obtain the ownership you need to
NSDate *newDate = [startDate addTimeInterval:86400 * sliderValueAsInt];
if (newDate != startDate) {
[startDate release];
startDate = [newDate retain];
}
which can be conveniently rewritten as
self.startDate = [startDate addTimeInterval:86400 * sliderValueAsInt];
Judging from the code logic, the startDate should be kept immutable. That means the startDate = newDate; stuff shouldn't appear at all. Delete the 3 lines below NSDate *newDate = [startDate ...]; and the code shall work fine.
Related
I have followed this tutorial
http://ios.biomsoft.com/2011/08/27/iphone-programming-tutorial-–-local-notifications/
I am able to run the app, however in order to run the app I have remove the following code in my appdelegate.m
You can see the error in the picture above.
Why do I receive the error and how do I fix the error?
Here is my code.
.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UITableViewDelegate,UITableViewDataSource> {
IBOutlet UITableView *tableview;
IBOutlet UIDatePicker *datePicker;
IBOutlet UITextField *eventText;
}
#property (nonatomic, retain) IBOutlet UITableView *tableview;
#property (nonatomic, retain) IBOutlet UIDatePicker *datePicker;
#property (nonatomic, retain) IBOutlet UITextField *eventText;
- (IBAction) scheduleAlarm:(id) sender;
#end
.m
#implementation ViewController
#synthesize datePicker,tableview, eventText;
- (IBAction) scheduleAlarm:(id) sender {
[eventText resignFirstResponder];
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
// Get the current date
NSDate *pickerDate = [self.datePicker date];
// Break the date up into components
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit |
NSMonthCalendarUnit | NSDayCalendarUnit )
fromDate:pickerDate];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit |
NSMinuteCalendarUnit | NSSecondCalendarUnit )
fromDate:pickerDate];
// Set up the fire time
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:[timeComponents hour]];
// Notification will fire in one minute
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:[timeComponents second]];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = itemDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
// Notification details
localNotif.alertBody = [eventText text];
// Set the action button
localNotif.alertAction = #"View";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
// Specify custom data for the notification
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:#"someValue"
forKey:#"someKey"];
localNotif.userInfo = infoDict;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[self.tableview reloadData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
datePicker = nil;
tableview = nil;
eventText = nil;
}
- (void)viewDidLoad{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// We only have one section
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of notifications
return [[[UIApplication sharedApplication] scheduledLocalNotifications] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier];
}
// Get list of local notifications
NSArray *notificationArray = [[UIApplication sharedApplication]
scheduledLocalNotifications];
UILocalNotification *notif = [notificationArray objectAtIndex:indexPath.row];
// Display notification info
[cell.textLabel setText:notif.alertBody];
[cell.detailTextLabel setText:[notif.fireDate description]];
return cell;
}
#end
You have to create an instance of the viewControllerfirst:
ViewController *viewController = [[ViewController alloc] init];
// set the frame, do other stuff..
[_window addSubview:viewController.view];
please check your AppDelegate code first
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
/******
YOUR CODE HERE
******/
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
Ok so after doing some playing around. I was able to get the app to run without having to change the app delegate at all.
Not sure why in the tutorial he states to do so.
Thanks for you help.
I would like to add the values in the strings together which are in the format:"HH:mm:ss.SSS" and display it back again. In my case, it is adding timeString with difference to get currentString. I am not sure of the correct way of doing it. I am definitely doing it wrongly. The problem i face is that when i click the stop button and click start again, it starts off from 0 again.. I would like it to start off from the same spot where it left off..
The portion of the code is here:
-(void)updateTimer{
currentDate = [NSDate date];
NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"HH:mm:ss.SSS"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
timeString=[dateFormatter stringFromDate:timerDate];
currentString=[dateFormatter stringFromDate:timerDate];
//double doubleOfString = [timeString doubleValue] + [difference doubleValue];
//currentString = [NSString stringWithFormat:#"%f",doubleOfString];
lbl.text = currentString;
}
The entire code is here:
Viewcontroller.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{
UILabel *lbl;
NSTimer *stopTimer;
NSDate *startDate,*currentDate;
BOOL running,lap;
UIButton *bttn;
NSMutableArray *tableItems;
NSString *timeString,*currentString,*difference;
UITableView *tableview;
}
#property (strong,nonatomic) IBOutlet UILabel *lbl;
#property (strong,nonatomic) IBOutlet UIButton *bttn;
#property (strong,nonatomic) NSMutableArray *tableItems;
#property (strong,nonatomic) NSString *timeString;
#property (strong,nonatomic) IBOutlet UITableView *tableview;
-(IBAction)startPressed:(id)sender;
-(IBAction)resetPressed:(id)sender;
-(void)updateTimer;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize lbl,bttn,tableItems,timeString,tableview;
- (void)viewDidLoad
{
[super viewDidLoad];
lbl.text = #"00.00.00.000";
running = FALSE;
lap = FALSE;
difference = #"0";
startDate = [NSDate date];
tableItems = [[NSMutableArray alloc] init];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)startPressed:(id)sender{
if(!running){
running = TRUE;
lap = TRUE;
[sender setTitle:#"Stop" forState:UIControlStateNormal];
[bttn setTitle:#"Lap" forState:UIControlStateNormal];
if (stopTimer == nil) {
stopTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
target:self
selector:#selector(updateTimer)
userInfo:nil
repeats:YES];
}
}else{
running = FALSE;
lap = FALSE;
startDate = currentDate;
difference = currentString;
[sender setTitle:#"Start" forState:UIControlStateNormal];
[bttn setTitle:#"Restart" forState:UIControlStateNormal];
[stopTimer invalidate];
stopTimer = nil;
}
}
-(void)updateTimer{
currentDate = [NSDate date];
NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"HH:mm:ss.SSS"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
timeString=[dateFormatter stringFromDate:timerDate];
currentString=[dateFormatter stringFromDate:timerDate];
double doubleOfString = [timeString doubleValue] + [difference doubleValue];
//currentString = [NSString stringWithFormat:#"%f",doubleOfString];
lbl.text = currentString;
}
-(IBAction)resetPressed:(id)sender{
if (!lap) {
[stopTimer invalidate];
stopTimer = nil;
tableItems = [[NSMutableArray alloc] init];
startDate = [NSDate date];
lbl.text = #"00.00.00.000";
running = FALSE;
}
else{
[tableItems insertObject:timeString atIndex:0];
[tableview reloadData];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return tableItems.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//Step 1:Check whether if we can reuse a cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
//Step2: If there are no new cells to reuse,create a new one
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:#"cell"];
//UIView *v = [[UIView alloc] init];
//v.backgroundColor = [UIColor redColor];
//cell.selectedBackgroundView = v;
//changing the radius of the corners
//cell.layer.cornerRadius = 10;
}
//Step 3: Set the cell text content
cell.textLabel.text = [tableItems objectAtIndex:indexPath.row];
//Step 4: Return the row
return cell;
}
#end
I welcome any other corrections that you can suggest...
I would kindly suggest not applying math to strings.
I mean:
What's the sum of the words “apple” and “Millenium Falcon”? Doesn’t make any sense, does it?
So why are you trying to form an addition of two strings, when you had a perfectly good number (timeInterval) — a type perfectly suitable to perform algebraic operations on?
PS:
NSDateFormatter instances want to be reused. Stash yours in an instance variable.
Your update timer function will be:Firstly add int counter in .h file . In viewDidLoad counter = 0;
-(void)updateTimer{
counter ++;
int hours = counter / 3600 ;
int minutes = (counter / 60) - (hours * 60);
int seconds = counter - (hours * 3600) - (minutes * 60);
lbl.text = [NSString stringWithFormat:#"%02d:%02d:%02d", hours, minutes, seconds];
}
I am making an Alarm clock in which i want that when the selected time through Datepicker occur instead of only notification and Badge occurance , I wanted to play my custom Sound file i.e. jet.wav (it is of less than 30 sec and in .wav format).i want that as soon as my notification occurs it should play a sound and when i click on the app or alert view then it should stop. So can anyone please help me out. here is what i am trying :-
Code:-
#class The420DudeViewController;
#interface The420DudeAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
The420DudeViewController *viewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet The420DudeViewController *viewController;
extern NSString *kRemindMeNotificationDataKey;
#implementation The420DudeAppDelegate
#synthesize window;
#synthesize viewController;
NSString *kRemindMeNotificationDataKey = #"kRemindMeNotificationDataKey";
#pragma mark -
#pragma mark === Application Delegate Methods ===
#pragma mark -
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
Class cls = NSClassFromString(#"UILocalNotification");
if (cls) {
UILocalNotification *notification = [launchOptions objectForKey:
UIApplicationLaunchOptionsLocalNotificationKey];
if (notification) {
NSString *reminderText = [notification.userInfo
objectForKey:kRemindMeNotificationDataKey];
[viewController showReminder:reminderText];
}
}
application.applicationIconBadgeNumber = 0;
[window addSubview:viewController.view];
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
application.applicationIconBadgeNumber = 0;
}
- (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification {
application.applicationIconBadgeNumber = 0;
NSString *reminderText = [notification.userInfo
objectForKey:kRemindMeNotificationDataKey];
[viewController showReminder:reminderText];
}
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
#end
#interface The420DudeViewController : UIViewController {
IBOutlet UINavigationBar *titleBar;
IBOutlet UIButton *setAlarmButton;
IBOutlet UIDatePicker *selectTimePicker;
AVAudioPlayer *player;
}
#property(nonatomic, retain) IBOutlet UINavigationBar *titleBar;
#property(nonatomic, retain) IBOutlet UIButton *setAlarmButton;
#property(nonatomic, retain) IBOutlet UIDatePicker *selectTimePicker;
-(IBAction)onTapSetAlarm;
- (void)showReminder:(NSString *)text;
#end
#implementation The420DudeViewController
#synthesize titleBar,setAlarmButton,selectTimePicker;
#pragma mark -
#pragma mark === Initialization and shutdown ===
#pragma mark -
- (void)viewDidLoad {
[super viewDidLoad];
selectTimePicker.minimumDate = [NSDate date];
}
- (void)viewDidUnload {
[super viewDidUnload];
self.setAlarmButton = nil;
self.selectTimePicker = nil;
}
/*
-(void)viewWillAppear:(BOOL)animated
{
NSString *path = [[NSBundle mainBundle]pathForResource:#"Song1" ofType:#"mp3"];
NSURL *url = [NSURL fileURLWithPath:path];
player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
// [player play];
}
*/
-(IBAction)onTapSetAlarm
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
Class cls = NSClassFromString(#"UILocalNotification");
if (cls != nil) {
UILocalNotification *notif = [[cls alloc] init];
notif.fireDate = [selectTimePicker date];
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.alertBody = #"Did you forget something?";
notif.alertAction = #"Show me";
notif.repeatInterval = NSDayCalendarUnit;
// notif.soundName = UILocalNotificationDefaultSoundName;
notif.soundName = [[NSBundle mainBundle]pathForResource:#"jet" ofType:#"wav"];
notif.applicationIconBadgeNumber = 1;
NSDictionary *userDict = [NSDictionary dictionaryWithObject:#"Mayank"
forKey:kRemindMeNotificationDataKey];
notif.userInfo = userDict;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
[notif release];
}
/*
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:#"HH:mm a"];
NSDate *selectedDate = [[NSDate alloc] init];
selectedDate = [selectTimePicker date];
NSString *theTime = [timeFormat stringFromDate:selectedDate];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Time selected" message:theTime delegate:nil cancelButtonTitle:#"YES" otherButtonTitles:nil];
[alert show];
[alert release];
// [timeFormat release];
// [selectedDate release];
*/
}
#pragma mark -
#pragma mark === Public Methods ===
#pragma mark -
- (void)showReminder:(NSString *)text {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Reminder"
message:#" TEXT " delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
[alertView release];
}
- (void)dealloc {
[super dealloc];
[titleBar release];
[setAlarmButton release];
[selectTimePicker release];
}
#end
According to the documentation for soundName, you should
specify the filename (including extension) of a sound resource in the application’s main bundle
Thus I suggest you change this line of -[The420DudeViewController onTapSetAlarm]:
notif.soundName = [[NSBundle mainBundle]pathForResource:#"jet" ofType:#"wav"];
to the following:
notif.soundName = #"jet.wav";
I have a table with all the users events in my app. When you click on one of the events I would like it to save the event name, location, start date, end date, notes in a UILabel. How can I do this my table code is bellow. I have tried searching the web for hours and nothing comes up.
Below is the code for the table
//eventsTable.h
#import <UIKit/UIKit.h>
#import <EventKit/EventKit.h>
#import <EventKitUI/EventKitUI.h>
#interface eventsTable : UIViewController <UITableViewDelegate, UITableViewDataSource, UIAlertViewDelegate,UIPopoverControllerDelegate,UINavigationControllerDelegate> {
IBOutlet UITableView *Table;
EKEventStore *eventStore;
EKEvent *event;
EKEventViewController *detailViewController;
EKCalendar *defaultCalendar;
NSMutableArray *eventsList;
}
-(IBAction) done;
- (NSArray *)fetchEventsForToday;
#property (nonatomic, retain)
IBOutlet UITableView *Table;
#property (nonatomic, retain) EKEventStore *eventStore;
#property (nonatomic, retain) EKCalendar *defaultCalendar;
#property (nonatomic, retain) NSMutableArray *eventsList;
#property (nonatomic, retain) EKEventViewController *detailViewController;
#end
//eventsTable.m
#import "eventsTable.h"
#implementation eventsTable
#synthesize eventsList, eventStore, defaultCalendar, detailViewController,Table;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
self.title = #"Events List";
// Initialize an event store object with the init method. Initilize the array for events.
self.eventStore = [[EKEventStore alloc] init];
self.eventsList = [[NSMutableArray alloc] initWithArray:0];
// Get the default calendar from store.
self.defaultCalendar = [self.eventStore defaultCalendarForNewEvents];
// Create an Add button
// Fetch today's event on selected calendar and put them into the eventsList array
[self.eventsList addObjectsFromArray:[self fetchEventsForToday]];
[Table reloadData];
}
-(IBAction) done{
[self dismissModalViewControllerAnimated:YES];
}
#pragma mark -
#pragma mark Table view data source
// Fetching events happening in the next 24 hours with a predicate, limiting to the default calendar
- (NSArray *)fetchEventsForToday {
NSDate *startDate1 = [NSDate date];
// endDate is 1 day = 60*60*24 seconds = 86400 seconds from startDate
NSDate *endDate1 = [NSDate distantFuture];
// Create the predicate. Pass it the default calendar.
NSArray *calendarArray = [NSArray arrayWithObject:defaultCalendar];
//NSPredicate *predicate = [self.eventStore predicateForEventsWithStartDate:startDate endDate:endDate
// calendars:calendarArray];
NSPredicate *predicate = [self.eventStore predicateForEventsWithStartDate:startDate1 endDate:endDate1 calendars:calendarArray];
// Fetch all events that match the predicate.
NSArray *events = [self.eventStore eventsMatchingPredicate:predicate];
events =
[events sortedArrayUsingSelector:
#selector(compareStartDateWithEvent:)];
self.eventsList = [NSMutableArray arrayWithArray:events];
[Table reloadData];
return events;
}
#pragma mark -
#pragma mark Table View
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return eventsList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *result = nil;
if ([tableView isEqual:self.Table] == YES){
static NSString *EventsCellIdentifier = #"Events";
/* We have the index path so let's get the corresponding
event from the array of events */
EKEvent *event1 = [self.eventsList
objectAtIndex:indexPath.row];
/* Try to get a reusable table cell */
result =
[tableView dequeueReusableCellWithIdentifier:EventsCellIdentifier];
if (result == nil){
result = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:EventsCellIdentifier] autorelease];
}
/* The title text of the cell will be the title of the event */
result.textLabel.text = event1.title;
result.textLabel.font = [UIFont boldSystemFontOfSize:16.0f];
result.detailTextLabel.font = [UIFont systemFontOfSize:12.0f];
/* Now let's format the date and the time of the event
and display it as the subtitle of the cell */
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components =
[calendar components:
NSYearCalendarUnit |
NSMonthCalendarUnit |
NSDayCalendarUnit |
NSHourCalendarUnit |
NSMinuteCalendarUnit |
NSSecondCalendarUnit
fromDate:event1.startDate];
NSDateComponents *components1 =
[calendar components:
NSYearCalendarUnit |
NSMonthCalendarUnit |
NSDayCalendarUnit |
NSHourCalendarUnit |
NSMinuteCalendarUnit |
NSSecondCalendarUnit
fromDate:event1.endDate];
if ([components hour] == 0 &&
[components minute] == 0 &&
[components second] == 0){
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar] autorelease];
NSDate *date = [calendar dateFromComponents: components1];
NSString *string = [NSDateFormatter localizedStringFromDate: date dateStyle: NSDateFormatterShortStyle timeStyle: NSDateFormatterNoStyle];
NSDate *date1 = [calendar dateFromComponents: components];
NSString *string1 = [NSDateFormatter localizedStringFromDate: date1 dateStyle: NSDateFormatterShortStyle timeStyle: NSDateFormatterNoStyle];
result.detailTextLabel.text =
[NSString stringWithFormat:#"All Day from %# - %#",string1,string,
(long)[components month],
(long)[components day],
(long)[components year],
(long)[components1 month],
(long)[components1 day],
(long)[components1 year]];
} else {
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar] autorelease];
NSDate *date = [calendar dateFromComponents: components1];
NSString *string = [NSDateFormatter localizedStringFromDate: date dateStyle: NSDateFormatterShortStyle timeStyle: NSDateFormatterShortStyle];
NSDate *date1 = [calendar dateFromComponents: components];
NSString *string1 = [NSDateFormatter localizedStringFromDate: date1 dateStyle: NSDateFormatterShortStyle timeStyle: NSDateFormatterShortStyle];
result.detailTextLabel.text =
[NSString stringWithFormat:#"%# - %#",string1,string,
(long)[components month],
(long)[components day],
(long)[components year],
(long)[components hour],
(long)[components minute],
(long)[components1 hour],
(long)[components1 minute]];
}
}
return(result);
[event release];
[eventsList release];
[eventStore release];
[result release];
}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
- (void)viewDidUnload {
self.eventsList = nil;
self.view = nil;
self.Table = nil;
[super viewDidUnload];
}
- (void)dealloc {
[super dealloc];
[Table release];
[eventStore release];
[eventsList release];
[defaultCalendar release];
[detailViewController release];
}
#end
Remove the [Table reloadData] and [self.eventsList addObjectsFromArray:[self fetchEventsForToday]]; from viewDidload and place it in
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
[self.eventsList addObjectsFromArray:[self fetchEventsForToday]];
return [self.eventsList count];
}
Hope this will help you. but it will be better to debug the method [self fetchEventsForToday].
I have an app and in one view is a table. In the table I call the EventStore So It fills the table with your calendar events. I would like to have bars in the table like the ical app on the iphone so It breaks the days up with A bar the bar has the date (ex. March 25, 2011). Below is the code for the table
//eventsTable.h
#import <UIKit/UIKit.h>
#import <EventKit/EventKit.h>
#import <EventKitUI/EventKitUI.h>
#interface eventsTable : UIViewController <UITableViewDelegate, UITableViewDataSource, UIAlertViewDelegate,UIPopoverControllerDelegate,UINavigationControllerDelegate> {
IBOutlet UITableView *Table;
EKEventStore *eventStore;
EKEvent *event;
EKEventViewController *detailViewController;
EKCalendar *defaultCalendar;
NSMutableArray *eventsList;
}
-(IBAction) done;
- (NSArray *)fetchEventsForToday;
#property (nonatomic, retain)
IBOutlet UITableView *Table;
#property (nonatomic, retain) EKEventStore *eventStore;
#property (nonatomic, retain) EKCalendar *defaultCalendar;
#property (nonatomic, retain) NSMutableArray *eventsList;
#property (nonatomic, retain) EKEventViewController *detailViewController;
#end
//eventsTable.m
#import "eventsTable.h"
#implementation eventsTable
#synthesize eventsList, eventStore, defaultCalendar, detailViewController,Table;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
self.title = #"Events List";
// Initialize an event store object with the init method. Initilize the array for events.
self.eventStore = [[EKEventStore alloc] init];
self.eventsList = [[NSMutableArray alloc] initWithArray:0];
// Get the default calendar from store.
self.defaultCalendar = [self.eventStore defaultCalendarForNewEvents];
// Create an Add button
// Fetch today's event on selected calendar and put them into the eventsList array
[self.eventsList addObjectsFromArray:[self fetchEventsForToday]];
[Table reloadData];
}
-(IBAction) done{
[self dismissModalViewControllerAnimated:YES];
}
#pragma mark -
#pragma mark Table view data source
// Fetching events happening in the next 24 hours with a predicate, limiting to the default calendar
- (NSArray *)fetchEventsForToday {
NSDate *startDate1 = [NSDate date];
// endDate is 1 day = 60*60*24 seconds = 86400 seconds from startDate
NSDate *endDate1 = [NSDate distantFuture];
// Create the predicate. Pass it the default calendar.
NSArray *calendarArray = [NSArray arrayWithObject:defaultCalendar];
//NSPredicate *predicate = [self.eventStore predicateForEventsWithStartDate:startDate endDate:endDate
// calendars:calendarArray];
NSPredicate *predicate = [self.eventStore predicateForEventsWithStartDate:startDate1 endDate:endDate1 calendars:calendarArray];
// Fetch all events that match the predicate.
NSArray *events = [self.eventStore eventsMatchingPredicate:predicate];
events =
[events sortedArrayUsingSelector:
#selector(compareStartDateWithEvent:)];
self.eventsList = [NSMutableArray arrayWithArray:events];
[Table reloadData];
return events;
}
#pragma mark -
#pragma mark Table View
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return eventsList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *result = nil;
if ([tableView isEqual:self.Table] == YES){
static NSString *EventsCellIdentifier = #"Events";
/* We have the index path so let's get the corresponding
event from the array of events */
EKEvent *event1 = [self.eventsList
objectAtIndex:indexPath.row];
/* Try to get a reusable table cell */
result =
[tableView dequeueReusableCellWithIdentifier:EventsCellIdentifier];
if (result == nil){
result = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:EventsCellIdentifier] autorelease];
}
/* The title text of the cell will be the title of the event */
result.textLabel.text = event1.title;
result.textLabel.font = [UIFont boldSystemFontOfSize:16.0f];
result.detailTextLabel.font = [UIFont systemFontOfSize:12.0f];
/* Now let's format the date and the time of the event
and display it as the subtitle of the cell */
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components =
[calendar components:
NSYearCalendarUnit |
NSMonthCalendarUnit |
NSDayCalendarUnit |
NSHourCalendarUnit |
NSMinuteCalendarUnit |
NSSecondCalendarUnit
fromDate:event1.startDate];
NSDateComponents *components1 =
[calendar components:
NSYearCalendarUnit |
NSMonthCalendarUnit |
NSDayCalendarUnit |
NSHourCalendarUnit |
NSMinuteCalendarUnit |
NSSecondCalendarUnit
fromDate:event1.endDate];
if ([components hour] == 0 &&
[components minute] == 0 &&
[components second] == 0){
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar] autorelease];
NSDate *date = [calendar dateFromComponents: components1];
NSString *string = [NSDateFormatter localizedStringFromDate: date dateStyle: NSDateFormatterShortStyle timeStyle: NSDateFormatterNoStyle];
NSDate *date1 = [calendar dateFromComponents: components];
NSString *string1 = [NSDateFormatter localizedStringFromDate: date1 dateStyle: NSDateFormatterShortStyle timeStyle: NSDateFormatterNoStyle];
result.detailTextLabel.text =
[NSString stringWithFormat:#"All Day from %# - %#",string1,string,
(long)[components month],
(long)[components day],
(long)[components year],
(long)[components1 month],
(long)[components1 day],
(long)[components1 year]];
} else {
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar] autorelease];
NSDate *date = [calendar dateFromComponents: components1];
NSString *string = [NSDateFormatter localizedStringFromDate: date dateStyle: NSDateFormatterShortStyle timeStyle: NSDateFormatterShortStyle];
NSDate *date1 = [calendar dateFromComponents: components];
NSString *string1 = [NSDateFormatter localizedStringFromDate: date1 dateStyle: NSDateFormatterShortStyle timeStyle: NSDateFormatterShortStyle];
result.detailTextLabel.text =
[NSString stringWithFormat:#"%# - %#",string1,string,
(long)[components month],
(long)[components day],
(long)[components year],
(long)[components hour],
(long)[components minute],
(long)[components1 hour],
(long)[components1 minute]];
}
}
return(result);
[event release];
[eventsList release];
[eventStore release];
[result release];
}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
- (void)viewDidUnload {
self.eventsList = nil;
self.view = nil;
self.Table = nil;
[super viewDidUnload];
}
- (void)dealloc {
[super dealloc];
[Table release];
[eventStore release];
[eventsList release];
[defaultCalendar release];
[detailViewController release];
}
#end
You are going to have to split each day up into a separate section and implement the proper UITableViewDataSource protocols for section indexes: http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html
Here is a tutorial: http://www.iphonedevcentral.com/indexed-uitableview-tutorial/
If I understand your question correctly, you need to change the section titles with the date you want to display. You do this by implementing the (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section method.
You have a problem in your code despite the question. You must release the resources before the return.
return(result);
[event release];
[eventsList release];
[eventStore release];
[result release];