iphone - 使用 NSFetchedResult Controller 处理节标题

标签 iphone objective-c ios ipad core-data

我是核心数据的新手,目前我正在使用核心数据模型构建应用程序。我想要实现的是一个表格 View ,每个月都有部分标题。目前我的应用程序正在运行,它显示每个单元格中的所有数据,但没有每个月的部分标题。我想要的是带有“日期”的对象:“26/08/2012”显示在标题为八月的部分下方。

我有一个包含以下数据的 json 网络服务。

 "date": "26/08/2012",
    "hour": "18u00",
    "type": "JPL",
    "home": "KRC Genk",
    "away": "Zulte-Waregem",
    "homeScore": "2",
    "awayScore": "0"

我构建了它的一个 NSManagedObject 子类,以及这个类的一个类别来将值存储在数据库中。这一切都有效。
为了将这些值放在表格 View 中,我有以下方法。
- (void)getKalender // attaches an NSFetchRequest to this UITableViewController
{
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Kalender"];
    request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES]];

    self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
                                                                        managedObjectContext:self.genkDatabase.managedObjectContext
                                                                          sectionNameKeyPath:nil
                                                                                   cacheName:nil];

}

这就是我在 cellForRowAtIndexPath 中所做的
static NSString *simpleTableIdentifier = @"KalenderCell";

        KalenderCell *cell = (KalenderCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
        if ((cell == nil) || (![cell isKindOfClass:KalenderCell.class]))
        {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"KalenderCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];
        }

        // ask NSFetchedResultsController for the NSMO at the row in question
        Kalender *kalender = [self.fetchedResultsController objectAtIndexPath:indexPath];
        // Then configure the cell using it ...

        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateFormat:@"dd/MM"];
       NSString *dateStr = [dateFormat stringFromDate:kalender.date];
        cell.lblType.text           = kalender.type;
        cell.lblHome.text           = kalender.home;
        cell.lblHomeScore.text      = kalender.homeScore;
        cell.lblAwayScore.text      = kalender.awayScore;
        cell.lblAway.text           = kalender.away;
        cell.lblDate.text           = dateStr;
        cell.lblHour.text           = kalender.hour;


        return cell;

但在这里我被困住了。谁能帮助我如何使用 NSFetchedResult Controller 设置这些部分标题?

提前谢谢了!

最佳答案

只需在 NSFetchedResultsController 初始的 sectionNameKeyPath 中设置要使用的字段的名称,如下所示:

self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:self.genkDatabase.managedObjectContext sectionNameKeyPath:@"date" cacheName:nil];

这应该将日期字符串设置为部分名称。随意使用您想要的任何字段。

关于iphone - 使用 NSFetchedResult Controller 处理节标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12790194/

相关文章:

iphone - makeKeyAndVisible & makeKeyWindow - iPhone 中的 uiwindow

ios - 获取ios7中的Twitter关注者并关注

objective-c - 什么用于核心数据排序?

ios - 如何通过从服务器获取数据来设置文本字段值

objective-c - 使用 initWithBitmapDataPlanes 创建 NSImage 时的 Cocoa 内存管理

iphone - 如何处理 iPhone 应用程序中的 NSError?

ios - UITableView 阴影不正确(未对齐)

iphone - 如何将 'Glow effect' 添加到 UIBarButtonItem 中?

iphone - 使用 segue 将数据传输到另一个 Controller 。从代码

ios - 从 bundle 的 Assets 文件夹中加载图像