iOS - 基于日期范围的 UITableView 部分 [英] iOS - UITableView Sections based on date ranges

查看:62
本文介绍了iOS - 基于日期范围的 UITableView 部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,我想知道是否可以解决.我有一个使用核心数据和 NSFetchedResultsController 的 UITableView.我的应用程序的很大一部分是根据日期组织事物.理想情况下,我想将 TableView 划分为基于 3 个日期范围的部分.这些范围将介于现在和 2 天之间、介于 2 天和 6 天之间以及 10 天及以后.用户有一个 UIDatePicker,当他们输入日期时,它会自动放入这些有组织的部分之一.现在我知道如何按每个日期轻松地将 tableview 划分为多个部分,但不知道如何做到这一点,因此每个部分都有一个时间范围.感谢任何可能提供帮助的人.

I've got an issue and I'm wondering if it's possible to solve. I have a UITableView that uses Core Data and NSFetchedResultsController. A big part of my app is organizing things based on dates. Ideally, I'd like to divide the TableView into sections based off of 3 date ranges. These ranges would be between now and 2 days, between 2 days and 6 days, and 10 days and beyond. The user has a UIDatePicker, and when they enter a date it would be automatically put into one of these organized sections. Now I know how to easily divide the tableview into sections by each single date, but not how to do it so each section has a time range. Thanks to anyone that might be able to help out.

推荐答案

这是我自己做的.最终结果是核心数据对象被分类为部分,每个部分为 1 天.多个对象可能位于一个部分.

Just did this myself. The end result is that core data objects are sorted into sections, each one being 1 day wide. Multiple objects may be in one section.

//at a point where you define your fetched results controller
//add @"sectionTitle" as sectionNameKeyPath:

//...
 NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:context sectionNameKeyPath:@"sectionTitle" cacheName:@"CacheName"];
//....


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {


    NSString* dateString =  [[[self.sleepQualityController sections] objectAtIndex:section] name];

    return dateString;



}



//add this to your managed object's header file:
@property(nonatomic,assign)NSString* sectionTitle;

//this goes into your managed object's implementation file

-(NSDate *)dateWithOutTime:(NSDate *)datDate
{
    if( datDate == nil ) {
        datDate = [NSDate date];
    }
    NSDateComponents* comps = [[NSCalendar currentCalendar] components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:datDate];
    return [[NSCalendar currentCalendar] dateFromComponents:comps];

}

-(NSString*)sectionTitle
{

        NSDateFormatter* dateFormaterWeekdayMonthDay = [[NSDateFormatter alloc] init];
        dateFormaterWeekdayMonthDay.dateStyle = NSDateFormatterLongStyle;

    return [NSString stringWithFormat:@"%@", [dateFormaterWeekdayMonthDay stringFromDate:[self  dateWithOutTime:self.date]] ];
}

这篇关于iOS - 基于日期范围的 UITableView 部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆