我正在为我的应用程序使用Vurig 自定义日历。我需要在日历上标记多个日期,但日期应该是动态的,即来自 json 文件(我正在使用JSONKit
)的数据,而不是硬编码的。我怎样才能做到这一点?如何遍历日历以将 json 文件中的日期添加到数组中?
这是我到目前为止的代码:
JSON:
{
"events": [
{
"event": {
"month": "10",
"day": "15",
"detail": "Some Detail"
}
},
{
"event": {
"month": "10",
"day": "25",
"detail": "Some blah"
}
}
]
}
代码:
-(void)calendarView:(VRGCalendarView *)calendarView switchedToMonth:(int)month targetHeight:(float)targetHeight animated:(BOOL)animated {
NSArray *dates;
id day, month;
JSONDecoder* decoder = [[JSONDecoder alloc] init];
NSData *cdata =[self getJSON];
NSDictionary* listDictionary = [decoder objectWithData:cdata];
NSArray* events =[listDictionary objectForKey:@"events"];
for (NSDictionary *event in events) {
NSDictionary *eventDetails = [event objectForKey:@"event"];
day = [eventDetails objectForKey:@"day"];
month = [eventDetails objectForKey:@"month"];
}
//need help here to mark multiple dates in the calendar if month = currentMonth
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSIslamicCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:day]; //data from json file
[components setMonth:month]; //data from json file
NSDate *date1 = [calendar dateFromComponents:components];
dates = [NSArray arrayWithObjects:date1, nil];
[calendarView markDates:dates];
}
- (NSData*) getJSON {
NSError *err = nil;
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSString *filePath = [bundle pathForResource:@"info" ofType:@"json"];
NSData *jsonData = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMappedIfSafe error:&err];
return jsonData;
}