在iOS中使用日历天作为UITableView数据源

时间:2013-06-16 16:05:39

标签: iphone ios uitableview

我对iOS开发很新,并且提出了一个简单的问题。我在一些示例应用程序开发中一直使用UITableView's进行练习,并注意到数据源往往是某种类型的集合,例如NSArray。看来这背后的原因是你可以将当前UITableViewCell的索引映射到数据源中的正确索引。

所以现在我终于开始研究我在开始学习Objective-C和iOS开发时想要做的项目。一个日历应用,列出UITableView中的日期事件。我的问题是,由于我从EKCalendar中的EKEventStore对象访问事件,并且每天都有多个日历不同的日历,您将如何使用UITableView's数据源进行设置?我原本刚刚创建了一个NSArray NSDates,它从当前一天向后跨越了三年,从当天开始向前三年,然后我可以将表视图的索引映射到此作为数据源。这听起来不像是这样做的正确方法,因为如果用户需要前进超过三年呢?我假设有一个更有效的庄园或更好的方法来做到这一点。

- (id)init {
    self = [super init];

    if (self) {
        //Get calendar access from the user.
        [self initCalendars];

        DateUtility *dateUtility = [[DateUtility alloc] init];

        NSMutableArray *dates = [[NSMutableArray alloc] init];

        //Build array of NSDates for sharing with the View Controller
        //This seems like the incorrect way to do this...
        //Backwards three years
        for (int date = -(365*3); date < 0; date++) {
            [dates addObject:[dateUtility adjustDate:[NSDate date] byNumberOfDays:date]];
        }

        //Forward three years
        for (int date = 0; date < (365*3); date++) {
            [dates addObject:[dateUtility adjustDate:[NSDate date] byNumberOfDays:date]];
        }
    }

    return self;
}

- (void)initCalendars {
    //respondsToSelector indicates iOS 6 support.
    if ([self.eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]) {
        //Request access to user calendar
        [self.eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
            if (granted) {
                NSLog(@"iOS 6+ Access to EventStore calendar granted.");
            } else {
                NSLog(@"Access to EventStore calendar denied.");
            }
        }];
    } else { //iOS 5.x and lower support if Selector is not supported
        NSLog(@"iOS 5.x < Access to EventStore calendar granted.");
    }

    //Store a reference to all of the users calendars on the system.
    self.calendars = [self.eventStore calendarsForEntityType:EKEntityTypeEvent];

    [self.eventStore reset];
}

如果您想查看我的所有代码所做的事情,这是adjustDate方法。

- (NSDate *)adjustDate:(NSDate *)date byNumberOfDays:(NSUInteger)numberOfDays {
    NSDateComponents *components = [[NSDateComponents alloc] init];
    components.day = numberOfDays;

    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

    return [calendar dateByAddingComponents:components toDate:date options:0];
}

用于尝试将事件存储中多个EKCalendars的数据用作单个UITableView的数据源的最佳设计模式是什么?如何将日历的日期设置为数据源,无论特定日期的事件数量是多少,或者与使用的日历无关?

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

我认为我找到了一个相当不错的方法,所以我会回答我的问题。

我的数据源实例是一个跨越12个月的NSDate数组。当我从数据源中提取NSDate引用时,它会监视正在使用的当前索引。当索引开始接近数组中的lastObject时,数据源模型继续进行并产生额外6个月的NSDates。

由于我增加了数据源中的对象数量(在我的模型中),我添加了一个名为setNeedsNumberOfItemsRefreshed的BOOL属性。然后我使用我的View Controller作为观察者设置KVO,观察setNeedsNumberOfItemsRefreshed属性。当我向数据源添加项目时,我设置了setNeedsNumberOfItemsRefreshed = YES,我的View Controller在我的表视图上调用reloadData来更新numberOfItemsInSection。到目前为止,对我来说似乎工作得相当好。