gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 13:11:45 25 4
gpt4 key购买 nike

我是 iOS 开发的新手,有一个快速的问题。我一直在一些示例应用程序开发中使用 UITableView's 进行练习,并注意到数据源往往是某种集合,例如 NSArray。这背后的原因似乎是您可以将当前 UITableViewCell 的索引映射到数据源中的正确索引。

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

- (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 的数据源时,最好的设计模式是什么?您将如何将日历的日期设置为数据源,而不管特定日期的事件数量如何,或者无论所使用的日历如何?

感谢您的帮助!

最佳答案

我想我已经找到了一个相当不错的方法来解决这个问题,所以我会回答我的问题。

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

由于我增加了数据源中的对象数量(在我的模型中),我添加了一个名为 setNeedsNumberOfItemsRefreshed 的 BOOL 属性。然后我设置了一个 KVO,我的 View Controller 作为观察者,观察 setNeedsNumberOfItemsRefreshed 属性。当我将项目添加到数据源时,我设置了 setNeedsNumberOfItemsRefreshed = YES,并且我的 View Controller 在我的 TableView 上调用 reloadData,它更新了 numberOfItemsInSection。到目前为止,似乎对我来说效果很好。

关于iphone - 在 iOS 中使用日历日作为 UITableView 数据源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17135177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com