2014-01-07 66 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:)以查找日期>=(或使用谓词过滤,然后从结果中获取第一项并获取索引)。