나는 tableview
달력에 약속이 있습니다. 매일 section
이 분리되어 있습니다. 당신은 지금 오늘의 section
또는 경우 tableview
스크롤 가장 가까운 하나에 오늘에 대한 section
이 없다는 것을 원하는 UITableView 오늘 날짜에 가장 가까운 섹션으로 스크롤
여기에 무슨 뜻인지 알 수 있습니다.
[tableView scrollToRowAtIndexPath:scrollIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
지금 내 정렬 약속 / 일을 포함하는
NSMutableDictionary
이 있습니다
은 내가 다음 코드 조각을 사용한다는 것을 알고있다. 내가 올바른 NSIndexpath
을 찾을 수있는 방법,
-(NSDictionary *)sortKalendar:(NSMutableArray *)appointments{
NSMutableDictionary *buffer = [[NSMutableDictionary alloc] init];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd-MM-yyyy"];
for (int i = 0; i < appointments.count; i++) {
Appointment *object = [appointments objectAtIndex:i];
NSString *date = [formatter stringFromDate:object.app_start];
if(!(date == NULL)){
NSLog(@"date is %@",date);
if ([buffer objectForKey:date]) {
[(NSMutableArray *)[buffer objectForKey:date] addObject:object];
} else {
NSMutableArray *mutableArray = [[NSMutableArray alloc] initWithObjects:object, nil];
[buffer setObject:mutableArray forKey:date];
}
}
}
NSDictionary *result = [NSDictionary dictionaryWithDictionary:buffer];
return result;
}
내 질문은 지금 : 당신은 아래의 기능을 볼 수 있습니까?
미리 감사드립니다. 순간
편집
나는 다음을 사용하고 있습니다. 그러나 무언가는 여전히 옳지 않습니다.
NSArray *keys = [dictAppointments allKeys];
NSLog(@"KEYS ARE %@",keys) ;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd-MM-yyyy"];
NSArray *sortedArray = [keys sortedArrayUsingComparator:^(id obj1, id obj2) {
NSDate *date1 = [formatter dateFromString:obj1];
NSDate *date2 = [formatter dateFromString:obj2];
NSNumber *interval1 = [NSNumber numberWithDouble:[date1 timeIntervalSinceNow]];
NSNumber *interval2 = [NSNumber numberWithDouble:[date2 timeIntervalSinceNow]];
return (NSComparisonResult)[interval1 compare:interval2];
}];
NSLog(@"Sorted Array %@",sortedArray);
NSString *closestDateString = [sortedArray objectAtIndex:0];
NSLog(@"Closest date string is %@",closestDateString);
int section = [keys indexOfObject:closestDateString];
NSIndexPath *scrollIndexPath = [NSIndexPath indexPathForRow:0 inSection:section];
[tableView scrollToRowAtIndexPath:scrollIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
이 나에게 다음과 같은 로그를 제공합니다 : 당신은 분류 날짜 배열을
2014-01-07 13:28:42.420 Adsolut[9579:60b] KEYS ARE (
"03-12-2013",
"20-12-2013",
"17-12-2013",
"05-01-2014",
"21-12-2013",
"31-12-2013",
"04-01-2014",
"06-01-2014",
"16-01-2014",
"29-12-2013",
"03-01-2014",
"11-01-2014",
"18-12-2013",
"31-01-2014"
)
2014-01-07 13:28:42.437 Adsolut[9579:60b] Sorted Array (
"03-12-2013",
"17-12-2013",
"18-12-2013",
"20-12-2013",
"21-12-2013",
"29-12-2013",
"31-12-2013",
"03-01-2014",
"04-01-2014",
"05-01-2014",
"06-01-2014",
"11-01-2014",
"16-01-2014",
"31-01-2014"
)
안녕하세요 답변 해주셔서 감사합니다. 하지만 그 순간에 나는 closestDateString으로 첫 데이트를 되찾았다. – Steaphann
@StefGeelen 배열이 정렬되지 않았습니까? 'keys'와'sortedArray'를 로그합니다.이것들은 같은가요? –
배열이 정렬됩니다. 그러나 지금 나는 그것을 원한다. 오늘 날짜에 가장 가까운 날짜를 원해. – Steaphann