核心数据对象在不同的​​tableView部分 [英] Core data objects in different tableView sections

查看:91
本文介绍了核心数据对象在不同的​​tableView部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个tableView可扩展/可折叠部分和固定部分标题,我想维护。
这是代码:

I have a tableView with expandable/collapsable sections and fixed section titles, which I want to maintain. This is the code to do that:

- (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:

But I want to populate the rows from a core data entity, this is my 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的右侧部分。我需要你的帮助和建议。

Core data entity is 'ToDoItem', and what I finally want is that each object appears inside the right section on the tableView. I need your help and advice.

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

To make my question a little bit clearer, I would create an attribute called tdDate, and this attribute should be the filtering key to decide under which section should the object be shown. Taking into account that an object with the current date will appear under the TODAY section today, tomorrow it should appear under the OVERDUE section...

推荐答案

一个很好的方法是在 ToDoItem 中添加一个新属性(例如 state 可以为 NSFetchedResultsController 使用 sectionNameKeyPath

A nice way to achieve is to add a new attribute in your ToDoItem (e.g. state) that you can use as sectionNameKeyPath for your NSFetchedResultsController.

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

I'll try to explain with a simple example.

想一个 Food 列出食物的实体。此实体具有名称类别。例如

Think to a Food entity that lists foods. This entity has a name and a category. e.g.

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

如果使用类别 $ c> sectionNameKeyPath ,你会发现两个部分。一个包含属于水果类别的食物,另一个包含蔬菜一个。

If you use category as a sectionNameKeyPath you will find two sections. One will contain foods that belong to Fruit category, the other will contain the Vegetable one.

同样适用于在你的实体中使用 state 属性(为了简单起见,它是一个字符串,但你可以使用数字和使用将每个数字映射到字符串值的映射)。我想这是正确的,因为每个项目都将有一个完成状态。 此外,通过这种方法,与每个状态相关联的值可以动态更改。这应该是模型的一部分,而不是控制器的一部分。
因此, state 可以假设这些可能值之一: @逾期 @今天 @明天 @Someday @Completed

The same applies if use a state property in your entity (for the sake of simplicity it's a string but you could just use numbers and using a map to map each number to a string value). I guess this it's correct since each item will have a completion state. In addition, by means of this approach values associated with each state can change dynamically. And this should be part of the model and not of the controller. Hence, state could assume one of these possible values: @"Overdue", @"Today", @"Tomorrow", @"Upcoming", @"Someday", @"Completed".

作为一个重要的注释(见Apple doc)

As an important note (see Apple doc)


如果控制器生成节,
数组中的第一个排序描述符用于将对象分组;它的键必须是
与sectionNameKeyPath相同,或者使用
键的相对排序必须与使用sectionNameKeyPath的相同。

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.

这篇关于核心数据对象在不同的​​tableView部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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