带部分的 UITableView

发布于 2024-11-08 20:52:10 字数 7507 浏览 0 评论 0原文

我有一个应用程序,在一个视图中是一张桌子。在表中,我将其称为 EventStore,因此它会用您的日历事件填充表。我想在表格中添加条形图,就像 iPhone 上的 ical 应用程序一样,这样它就可以用 A 条形图来划分日期,条形图上有日期(例如 2011 年 3 月 25 日)。下面是表的代码

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暮光沉寂 2024-11-15 20:52:10

您必须将每一天分成一个单独的部分,并为部分索引实现正确的 UITableViewDataSource 协议: http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html

这是一个教程:http://www.iphonedevcentral.com/indexed-uitableview-tutorial/

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/

清风不识月 2024-11-15 20:52:10

如果我正确理解您的问题,您需要使用要显示的日期更改部分标题。您可以通过实现 (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 方法来完成此操作。

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.

青春有你 2024-11-15 20:52:10

尽管有问题,但您的代码存在问题。返回前必须释放资源。

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

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文