ios - 不同tableView部分的核心数据对象

标签 ios uitableview core-data

我有一个带有可扩展/可折叠部分和固定部分标题的 tableView,我想维护它。 这是执行此操作的代码:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 6;
}


+ (NSString*) titleForHeaderForSection:(int) section
{
    switch (section)
    {
        case 0 : return @"Overdue";
        case 1 : return @"Today";
        case 2 : return @"Tomorrow";
        case 3 : return @"Upcoming";
        case 4 : return @"Someday";
        case 5 : return @"Completed";
       // default : return [NSString stringWithFormat:@"Section no. %i",section + 1];
    }
}

目前,我使用以下代码填充行:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section)
    {
        case 2 : return 1;
        case 3 : return 30;
        default : return 8;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell.

    switch (indexPath.row)
    {
        case 0 : cell.textLabel.text = @"First Cell"; break;
        case 1 : cell.textLabel.text = @"Second Cell"; break;
        case 2 : cell.textLabel.text = @"Third Cell"; break;
        case 3 : cell.textLabel.text = @"Fourth Cell"; break;
        case 4 : cell.textLabel.text = @"Fifth Cell"; break;
        case 5 : cell.textLabel.text = @"Sixth Cell"; break;
        case 6 : cell.textLabel.text = @"Seventh Cell"; break;
        case 7 : cell.textLabel.text = @"Eighth Cell"; break;
        default : cell.textLabel.text = [NSString stringWithFormat:@"Cell %i",indexPath.row + 1];
    }

    //cell.detailTextLabel.text = ...;

    return cell;
}

但我想从核心数据实体填充行,这是我的 NSFetchedResultsController:

- (NSFetchedResultsController *)fetchedResultsController {

    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription
                                   entityForName:@"ToDoItem" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    NSSortDescriptor *sort = [[NSSortDescriptor alloc]
                              initWithKey:@"tdText" ascending:NO];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];

    [fetchRequest setFetchBatchSize:20];

    NSFetchedResultsController *theFetchedResultsController =
    [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                        managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil
                                                   cacheName:@"Root"];
    self.fetchedResultsController = theFetchedResultsController;
    _fetchedResultsController.delegate = self;

    return _fetchedResultsController;

}

核心数据实体是“ToDoItem”,我最终想要的是每个对象都出现在 tableView 的正确部分内。我需要你的帮助和建议。


为了让我的问题更清楚一点,我会创建一个名为 tdDate 的属性,这个属性应该是决定对象应该显示在哪个部分的过滤键。考虑到具有当前日期的对象今天将出现在 TODAY 部分下,明天它应该出现在 OVERDUE 部分下......

最佳答案

一个很好的实现方式是在您的 ToDoItem 中添加一个新属性(例如 state),您可以将其用作 sectionNameKeyPath NSFetchedResultsController

我将尝试用一个简单的例子来解释。

考虑一个列出食物的 Food 实体。该实体有一个名称 和一个类别。例如

Apple, Fruit
Banana, Fruit
Potato, Vegetable
Salad, Vegetable

如果您使用 category 作为 sectionNameKeyPath,您会发现两个部分。一个将包含属于 Fruit 类别的食物,另一个将包含属于 Vegetable 的食物。

如果在您的实体中使用 state 属性,这同样适用(为简单起见,它是一个字符串,但您可以只使用数字并使用映射将每个数字映射到一个字符串值)。我想这是正确的,因为每个项目都有一个完成状态。 此外,通过这种方法,与每个状态相关的值可以动态变化。这应该是模型的一部分而不是 Controller 的一部分。 因此,state 可以采用以下可能值之一:@"Overdue"@"Today"@"Tomorrow", @"Upcoming", @"Someday", @"Completed".

作为重要说明(请参阅 Apple 文档)

If the controller generates sections, the first sort descriptor in the array is used to group the objects into sections; its key must either be the same as sectionNameKeyPath or the relative ordering using its key must match that using sectionNameKeyPath.

关于ios - 不同tableView部分的核心数据对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21048835/

相关文章:

ios - 尝试获取实体属性的值时出错

ios - 如何修复世博会开始抛出 "Simulator is installed but is identified as ' com.apple.CoreSimulator.SimulatorTrampoline';不知道那是什么”

iphone - UITableView 给出空表,不加载数据

Cocoa:CoreData - ManagedObjectContext 中的多个实体

swift - tableView.reloadData() 导致应用程序崩溃

ios - 开关在 SMSegmentViev 中不起作用

iphone - UITableView "Scroll down to update"

ios - 表格内的动态大小标签在 ios 中不刷新

ios - 在 sendMessage : reply handler 中将 NSManagedObject 数组发送到 watchOS

ios - Fabric API key 无效