2014-01-07 1 views
3

나는 tableview 달력에 약속이 있습니다. 매일 section이 분리되어 있습니다. 당신은 지금 오늘의 section 또는 경우 tableview 스크롤 가장 가까운 하나에 오늘에 대한 section이 없다는 것을 원하는 UITableView 오늘 날짜에 가장 가까운 섹션으로 스크롤

enter image description here

여기에 무슨 뜻인지 알 수 있습니다.
[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" 
) 

답변

2

당신은 약속 날짜와 현재 날짜 사이의 시간 간격을 사용하여 배열을 정렬 할 수 있습니다,

NSArray *keys = [yourDictionary allKeys]; 

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:abs([date1 timeIntervalSinceNow])]; 
    NSNumber *interval2 = [NSNumber numberWithDouble:abs([date2 timeIntervalSinceNow])]; 
    return (NSComparisonResult)[interval1 compare:interval2]; 
}]; 

그리고에 의해 가장 가까운 날짜,

NSString *closestDateString = [sortedArray objectAtIndex:0]; 

그리고 그에서

,

int section = [keys indexOfObject:closestDateString]; 
NSIndexPath *scrollIndexPath = [NSIndexPath indexPathForRow:0 inSection:section]; 
+0

안녕하세요 답변 해주셔서 감사합니다. 하지만 그 순간에 나는 closestDateString으로 첫 데이트를 되찾았다. – Steaphann

+0

@StefGeelen 배열이 정렬되지 않았습니까? 'keys'와'sortedArray'를 로그합니다.이것들은 같은가요? –

+0

배열이 정렬됩니다. 그러나 지금 나는 그것을 원한다. 오늘 날짜에 가장 가까운 날짜를 원해. – Steaphann

0

을, 당신은하지 않습니다? dateArray이라고합시다.

하세요 오늘 날짜 개체 : 가장 가까운 날짜에 dateArray에 대한 NSDate * today = [NSDate date];

검색, 가장 가까운 날짜의 인덱스를 얻을.

NSDate * today = [NSDate date] ; 
__block NSUInteger section = NSNotFound ; 
__block NSTimeInterval timeInterval = NSTimeIntervalSince1970 ; 
[dateArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 
    NSDate * date = obj ; 
    NSTimeInterval ti = [today timeIntervalSinceDate:date] ; 
    if (ti < 0) 
     ti = -ti ; 
    if (ti <= timeInterval) { 
     section = idx ; 
     timeInterval = ti ; 
    } else { 
     *stop = YES ; 
    } 
}] ; 

당신은 지금 섹션 인덱스를 알고, 그래서 NSIndexPath는 (에서 날짜의 목록을 가져옵니다 [NSIndexPath indexPathForRow:0 inSection:sectionIndex]

0

이며, 섹션의 첫 번째 행의 인덱스는 0

이다의이 sectionIndex로 가정 해 봅시다 사전 또는 이미 가지고있는 사전). 그것을 정렬하십시오. 루프 (indexOfObjectPassingTest:)를 사용하면 오늘 >= 날짜를 찾을 수 있습니다. 또는 필터링 할 조건부를 사용하고 결과에서 첫 번째 항목을 가져 와서 색인을 가져옵니다.

관련 문제