gpt4 book ai didi

ios - 从 NSDate 获取 UITableView 标题

转载 作者:行者123 更新时间:2023-11-29 12:26:35 25 4
gpt4 key购买 nike

我正在通过 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 转换为字符串并将其分配给 header 标题:

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

表格 View 的显示方式如下:

enter image description here

数据似乎正确分组和排序。章节标题出现在按时间顺序排列的适当位置。但是标题标题没有出现。

当我将此代码与相关 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)

众所周知,我会做一些愚蠢的事情,但我已经找了 2 天但没有成功。

有什么想法吗?

最佳答案

这里的问题是每个 section在您获取的结果 Controller 中不是日期对象,因为您在这里处理它:

for (NSDate *headerDate in [transactionFRC sections])

相反,[transactionFRC sections]实际上返回一个 id<NSFetchedResultsSectionInfo> 的数组对象。这些包含对象列表、对象计数和名称。在您的情况下,每个部分的名称将是字符串形式的日期。要对此进行测试,请更换您的 viewDidLoad代码如下:

for (id<NSFetchedResultsSectionInfo> section in [transactionFRC sections])
{
NSLog(@"The sortDate is %@", section.name);
}

但是,这个日期的格式不是您想要的。你真的有两个选择:

  1. 首先使用 dateFormatter 将节的名称(字符串)转换为 NSDate对象,然后将该日期对象重新格式化为所需格式的字符串。

  1. 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 中对其进行测试,但希望它能帮到你。

关于ios - 从 NSDate 获取 UITableView 标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28930732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com