我正在通过MagicalRecord填充来自Core Data的数据的tableview。我希望tableview按时间顺序显示,按日期排序,也按时间顺序显示。
首先,我从transDate中获取sortDate,来自我在网上找到的修改后的代码:
// Gives beginning of this day
- (NSDate *)sortDateForDate:(NSDate *)inputDate
{
// Use the current calendar and time zone
NSCalendar *calendar = [NSCalendar currentCalendar];
NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
[calendar setTimeZone:timeZone];
// Selectively convert the date components (year, month, day) of the input date
NSDateComponents *dateComps = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:inputDate];
// Set the time components manually
[dateComps setHour:0];
[dateComps setMinute:0];
[dateComps setSecond:0];
// Convert back
NSDate *beginningOfDay = [calendar dateFromComponents:dateComps];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterFullStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
NSString *beginningOfDayText = [formatter stringFromDate:beginningOfDay];
NSLog(@"The sortDate should be %@",beginningOfDayText);
return beginningOfDay;
}
然后我汇编并保存新的交易:
-(void) assembleAndSaveTransaction
{
NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];
self.thisTransaction.transDate = [NSDate date];
self.thisTransaction.paidTo = self.noteField.text;
self.thisTransaction.forWhat = self.forWhatField.text;
// Call to obtain sortDate, method above
self.thisTransaction.sortDate = [self sortDateForDate:self.thisTransaction.transDate];
[localContext MR_saveToPersistentStoreAndWait];
}
接下来,我获取所有交易,按属性“sortDate”分组,并按属性“transDate”排序。
- (void)viewDidLoad
{
[super viewDidLoad];
transactionFRC = [WMMGTransaction MR_fetchAllGroupedBy:@"sortDate" withPredicate:nil sortedBy:@"transDate" ascending:NO];
}
然后,我将sortDate转换为字符串并将其分配给标题标题:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterFullStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
NSString *sectionLabel = [formatter stringFromDate:[transactionFRC.sections objectAtIndex:section]];
NSLog(@"The sortDate is %@",[formatter stringFromDate:[transactionFRC.sections objectAtIndex:section]]);
NSLog(@"The sectionLabel should be %@",sectionLabel);
return sectionLabel;
}
以下是tableview的显示方式:
数据似乎正在分组和排序。节标题出现在按时间顺序排列的适当位置。但标题标题不会出现。
当我将此代码放在具有相关TableView的VC的viewDidLoad方法中时:
NSLog(@"The header dates are as follows:\n");
for (NSDate *headerDate in [transactionFRC sections])
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterFullStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
NSLog(@"The sortDate is %@",[formatter stringFromDate:headerDate]);
}
我看到了这个读数:
2015-03-08 12:06:00.502 WheredMyMoneyGo[5374:7322757] The header dates are as follows:
2015-03-08 12:06:00.503 WheredMyMoneyGo[5374:7322757] The sortDate is (null)
2015-03-08 12:06:00.503 WheredMyMoneyGo[5374:7322757] The sortDate is (null)
2015-03-08 12:06:00.503 WheredMyMoneyGo[5374:7322757] The sortDate is (null)
2015-03-08 12:06:00.503 WheredMyMoneyGo[5374:7322757] The sortDate is (null)
我知道做蠢事,但我一直在寻找这个,但没有成功。
有什么想法吗?
答案 0 :(得分:1)
这里的问题是,您获取的结果控制器中的每个section
都不是日期对象,因为您在此处理它:
for (NSDate *headerDate in [transactionFRC sections])
相反,[transactionFRC sections]
实际上返回了id<NSFetchedResultsSectionInfo>
个对象的数组。它们包含对象列表,对象计数和名称。在您的情况下,每个部分的名称将是字符串形式的日期。要对此进行测试,请使用以下代码替换viewDidLoad
代码:
for (id<NSFetchedResultsSectionInfo> section in [transactionFRC sections])
{
NSLog(@"The sortDate is %@", section.name);
}
但是,此日期的格式不是您想要的样式。你真的有两个选择:
NSDate
对象,然后将该日期对象重新格式化为所需格式的字符串。OR
在titleForHeaderInSection:
中获取相关部分中的第一个对象(如果该部分中有对象),并将其排序日期格式化为字符串并使用它。这是一个例子:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
static NSDateFormatter *formatter = nil;
if (!formatter) {
formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterFullStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
}
id<NSFetchedResultsSectionInfo> sectionInfo = transactionFRC.sections[section];
WMMGTransaction *transaction = [sectionInfo.objects firstObject];
NSString *sectionLabel = [formatter stringFromDate:transaction.sortDate];
NSLog(@"The sortDate is %@", transaction.sortDate);
NSLog(@"The sectionLabel is %@",sectionLabel);
return sectionLabel;
}
我没有在Xcode中测试它就输入了这个,但希望它能让你到达某个地方。