簡體   English   中英

UITableView滾動到最接近今天日期的部分

[英]UITableView scroll to section that is closest to todays date

我有一個tableview ,里面有日程表。 每一天都是一個單獨的section 你可以在這看到我的意思

在此輸入圖像描述

現在我希望tableview滾動到今天的section ,或者如果今天沒有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"
)

您可以使用約會日期和當前日期之間的時間間隔對數組進行排序,

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

你有一個排序日期數組,不是嗎? 我們把它說成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]

獲取日期列表(從您的字典或已有的字典)。 解決。 循環遍歷它( indexOfObjectPassingTest:以查找日期>=今天(或使用謂詞進行過濾,然后從結果中獲取第一項並獲取索引)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM