2014-01-07 65 views
3

我有一個tableview日曆它的約會。每天是一個單獨的section。 你可以明白我的意思在這裏UITableView滾動到最接近今天的日期部分

enter image description here

現在我想的是,tableview滾動到今天的section或者如果沒有section今天到最近的一個。

我知道,我應該使用下面的代碼:

[tableView scrollToRowAtIndexPath:scrollIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES]; 

現在我有一個包含我的排序預約/天NSMutableDictionary。你可以看到下面的功能:

-(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; 
} 

我的問題是,現在,我怎麼能找到正確的NSIndexpath

在此先感謝!

編輯

目前我使用的是以下內容。但是有些東西還是不對的。

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

你好感謝你的回答。但目前我仍然將第一個日期作爲nearestDateString返回。 – Steaphann

+0

@StefGeelen數組是不是排序?記錄'keys'和'sortedArray'。這些都一樣嗎? –

+0

該數組已排序。但現在我想要的方式。我想要的日期最接近今天的日期。 – Steaphann

0

你有一個排序的日期排列,不是嗎?我們假設它爲dateArray

取得今天的日期對象:NSDate * today = [NSDate date];

搜索dateArray到最近的日期,拿到最近的日期的指標。

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 ; 
    } 
}] ; 

你現在知道的部分指標,讓我們說這是sectionIndex,在節中的第一行指數爲0。

所以NSIndexPath是[NSIndexPath indexPathForRow:0 inSection:sectionIndex]

0

獲取日期列表(由你的字典或你已經擁有的字典)。把它分類。循環播放(indexOfObjectPassingTest:)以查找日期>=(或使用謂詞過濾,然後從結果中獲取第一項並獲取索引)。