1

我已经从共享的谷歌日历 i IOS 中检索了我的事件,并在表格视图中显示它们。至于现在,它们只是简单地以事件标题作为标题,事件时间作为副标题来显示。我想改用日期(事件时间)作为部分标题,以便例如 3 月 31 日的所有事件都显示在一个组中,时间作为“正确的细节”

我的代码如下所示:

@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

我在网上找到了这段代码,但我相信他没有以 JSON 格式获取数据,而是来自 iPhone 日历。

@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

有没有办法让我使用我的 json event.startdate 作为部分标题?

4

2 回答 2

0

就个人而言,我会忽略您第二次发布的代码,因为它不是您想要做的。

您设置标题和副标题的代码部分是这两行: cell.textLabel.text = [NSString stringWithFormat:@"%@",event.title]; cell.detailTextLabel.text = [格式化字符串FromDate:gmtDate];

因此,如果您希望标题为日期,只需将其放置为 cell.textLabel.text 值。

于 2013-03-25T22:47:07.740 回答
0

所以如果你想做部分。

确保节数大于 1。

然后你可能想看看这些表方法。只需根据您的具体情况和定制需求,您可以相应地进行更改。

(NSString*) tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section{
   }

(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section{
 }

(UIView*)tableView:(UITableView*)table viewForHeaderInSection:(NsInteger)section{
}

我希望这有帮助。我通常会有一个例子,我只是目前没有最多的可用时间,但如果需要,我可以提供一个。

小心 :)

于 2013-03-25T23:16:11.983 回答