2011-05-21 154 views
1

我有一個應用程序,在一個視圖是一個表。在表格中,我調用EventStore So It使用日曆事件填充表格。我想在桌面上放置酒吧,就像iphone上的應用程序一樣,所以它用酒吧中有酒吧的日期(例如2011年3月25日)打破了日子。下面是表的代碼UITableView與部分

//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 

回答

2

你將有多達每天分成不同的部分,爲段索引實現適當的UITableViewDataSource協議:http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html

這裏是一個教程:http://www.iphonedevcentral.com/indexed-uitableview-tutorial/

+0

我一直在努力,沒有運氣。我不明白這是否適合變化的變量。我上面發佈了我的代碼,可以告訴我我是如何實現這一點的。謝謝。 – BDGapps 2011-05-24 00:56:38

2

如果我正確理解您的問題,則需要更改要顯示日期的節標題。您可以通過執行(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)部分方法來執行此操作。

+0

但我該如何使用if(section == 0) return @「要訪問的國家/地區」; else return @「Countries visited」; }如果它改變了,因爲它與更新的其他數組有關。 – BDGapps 2011-05-21 18:09:35

+0

以及我的猜測是,你將不得不想出一種方法來識別給定日期的事件,並創建一個有事件日期的數組。然後,當您使用事件填充表格時,可以按日期將事件分成多個部分,並使用日期數組中的相應日期標記這些部分。 – HM1 2011-05-21 18:18:52

0

儘管存在問題,但代碼中存在問題。您必須在返回之前釋放資源。

return(result); 
[event release]; 
[eventsList release]; 
[eventStore release]; 
[result release]; 
+0

您應該在評論中聲明,而不是答案,隊友... – vaxquis 2012-12-12 02:27:23

+0

我沒有看到添加評論鏈接。 – 2012-12-12 11:07:40