Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 获取对象并将其排序到节中_Ios_Core Data - Fatal编程技术网

Ios 获取对象并将其排序到节中

Ios 获取对象并将其排序到节中,ios,core-data,Ios,Core Data,在我的tableView中,我有固定的节和固定的节标题。这是我的这部分要求的代码: - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 6; } + (NSString*) titleForHeaderForSection:(int) section { switch (section) { case 0 : return @"Overdue";

在我的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];
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [CollapsableTableViewViewController titleForHeaderForSection:section];
}
这部分工作正常,但现在我想用核心数据对象填充这些部分。 对于这一部分,在获取结果之前,我有以下代码:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSError *error = nil;
    if (![[self fetchedResultsController] performFetch:&error])
    {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }


    switch (section)
    {
        case 2 : return 3;
        case 3 : return 30;
        default : return 3;
    }
}

- (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;
}
这也可以很好地填充节…但我真正的需求是使用核心数据实体中的对象填充节。这意味着,我必须替换以下行:

switch (section)
        {
            case 2 : return 3;
            case 3 : return 30;
            default : return 3;
        }
使用其他代码行,以获得节中的实际行数。 以及以下代码行:

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];
        }
用其他代码行替换以调用真实对象属性

我已经在y项目中包含了所有的核心数据堆栈和一个NSFetchedResultsController。 核心数据实体有两个属性:tdText、tdDate。 固定部分与tdDate属性相关

在这个代码示例中(除了Martin的建议之外),您正在
-tableView:numberofrowsinssection:
方法中获取数据。这是令人难以置信的花费,而且绝对是一个错误的地方。在应用程序的生命周期中,该方法会被调用数百次(如果不是数千次的话),您应该永远不会在那里执行提取

其次,在
NSFetchedResultsController上调用
-performFetch:
一次。这通常是在
-viewDidLoad
中进行的,理想情况下只调用一次。从那里,您可以询问
NSFetchedResultsController其当前状态;您不需要再次调用
-performFetch:`

至于其余的。在前进之前,您需要后退一步,填补您对Objective-C和核心数据的知识空白。这里一些评价最高的人给了你极好的答案,但你没有充分利用它们。如果你继续沿着这条路走下去,人们就会停止帮助你


在问另一个已经回答过的问题之前,请充分利用给出的建议。

首先将硬编码的内容放入一些更好的结构中,以便可以轻松地从核心数据填充一些数据

将以下内容添加到viewController的.h文件中

NSArray *sectionsArray;
NSMutableDictionary *sectionMenus;
BOOL                _searched;
BOOL                _loading;
在.m文件中添加以下内容

        #define REMINDER_OVERDUE  @"Overdue"
        #define REMINDER_TODAY    @"Today"
        #define REMINDER_TOMORROW @"Tomorrow"
        #define REMINDER_UPCOMING @"Upcoming"
        #define REMINDER_SOMEDAY  @"Someday"
        #define REMINDER_COMPLETE @"Complete"

        #define SEARCHING @"Searching..."


        - (void)viewDidLoad
        {
            [super viewDidLoad];

            // This dictionary will be used to store separate Arrays of records for each section using the section title as the key
            sectionMenus = [[NSMutableDictionary alloc] init];

            // These are the hard coded sections
            sectionsArray = [[NSArray alloc] initWithObjects: REMINDER_OVERDUE, REMINDER_TODAY, REMINDER_TOMORROW, REMINDER_UPCOMING, REMINDER_SOMEDAY, REMINDER_COMPLETE, nil];

            // We start by populating the sections with a single record containing the string "Searching..." so the user
            // will see that the section is busy searching for data to display
            [sectionMenus setObject:[[NSArray alloc] initWithObjects: SEARCHING, nil] forKey: REMINDER_OVERDUE];
            [sectionMenus setObject:[[NSArray alloc] initWithObjects: SEARCHING, nil] forKey: REMINDER_TODAY];
            [sectionMenus setObject:[[NSArray alloc] initWithObjects: SEARCHING, nil] forKey: REMINDER_TOMORROW];
            [sectionMenus setObject:[[NSArray alloc] initWithObjects: SEARCHING, nil] forKey: REMINDER_UPCOMING];
            [sectionMenus setObject:[[NSArray alloc] initWithObjects: SEARCHING, nil] forKey: REMINDER_SOMEDAY];
            [sectionMenus setObject:[[NSArray alloc] initWithObjects: SEARCHING, nil] forKey: REMINDER_COMPLETE];

            _searched = NO;
        }

        - (void)viewDidAppear:(BOOL)animated
        {
            [super viewDidAppear:animated];

            // Each time the view appears we check to see if a search has been done and if not then perform a search
            if (!_searched) {
                _searched=YES;
                [self getMenus];
            }
        }

        // tableView delegate methods to use the array and dictionary as the data sources
        - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
            return [sectionsArray count];
        }
        - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

            return [[sectionMenus objectForKey:[sectionsArray objectAtIndex:section]] count];
        }
        - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
            return [[sectionsArray objectAtIndex:section] description];
        }
        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
        {
            static NSString *simpleTableIdentifier = @"Cell";
            static NSString *inactiveTableIdentifier = @"InactiveTableItem";
            UITableViewCell *cell;

            NSObject *node =[[sectionMenus objectForKey:[sectionsArray objectAtIndex:[indexPath section]]] objectAtIndex:[indexPath row]];

            cell = [tableView dequeueReusableCellWithIdentifier:inactiveTableIdentifier];

            if (cell == nil) {
               cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:inactiveTableIdentifier];
            }

            cell.textLabel.text = [node description];

            return cell;
        }

        // This methods performs the queries to get the data for each section
        // and loads each section when its done (ideally should be done in background)
        - (void)getMenus
        {   
            _loading = YES;

            [self makeOverdueMenus];
            UITableView *tableView = (UITableView *)self.view;
            [tableView reloadSections:[[NSIndexSet alloc] initWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];


            [self makeTodaysMenus];
            [tableView reloadSections:[[NSIndexSet alloc] initWithIndex:1] withRowAnimation:UITableViewRowAnimationAutomatic];

            [self makeTomorrowsMenus];
            [tableView reloadSections:[[NSIndexSet alloc] initWithIndex:2] withRowAnimation:UITableViewRowAnimationAutomatic];

            _loading = NO;
            [tableView reloadData];
        }

        - (void)makeOverdueMenus {
            NSPredicate *predicate = [NSPredicate predicateWithFormat:@"dueDate < %@",[NSDate date]];

            [self makeMenu: REMINDER_OVERDUE predicate:predicate];
        }
        - (void)makeTodaysMenus {
            NSPredicate *predicate = [NSPredicate predicateWithFormat:@"dueDate >= %@ and dueDate < %@",[NSDate date], [NSDate date]];

            [self makeMenu: REMINDER_TODAY predicate:predicate];
        }
        - (void) makeTomorrowsMenus {
            NSPredicate *predicate = [NSPredicate predicateWithFormat:@"dueDate >= %@ and dueDate <= %@",[NSDate date], [NSDate date]];

            [self makeMenu: REMINDER_TOMORROW predicate:predicate];
        }

        // Helper function to get the section items and add them to the dictionary 
        - (void)makeMenu:(NSString*)section predicate:(NSPredicate*)predicate  {

                NSMutableArray *reminders = [[NSMutableArray alloc] init];
                [reminders addObjectsFromArray:[self getDataSimple];
                [sectionMenus setObject:reminders forKey:section];

        }

    // Simple method to fetch data from Core Data store
    - (NSArray*)getDataSimple {

       // replace this line with a core data query
       NSArray * results = [[NSArray alloc] initWithObjects:@"First Cell", @"Second Cell", @"Third Cell", nil];

       return results;

    }
#定义提醒#u过期@“过期”
#定义“今天”@“今天”
#定义“明天”@“明天”
#定义提醒_即将到来@“即将到来”
#定义“某天”@“某天”
#定义提醒_COMPLETE@“COMPLETE”
#定义搜索@“搜索…”
-(无效)viewDidLoad
{
[超级视图下载];
//此字典将用于使用节标题作为键存储每个节的单独记录数组
sectionMenus=[[NSMutableDictionary alloc]init];
//这些是硬编码部分
sectionsArray=[[NSArray alloc]initWithObjects:催款单过期,今天催款单,明天催款单,即将催款单,某天催款单,催款单完成,无];
//我们首先用一条包含字符串“Searching…”的记录填充各个部分,这样用户
//将看到该部分正忙于搜索要显示的数据
[sectionMenus setObject:[[NSArray alloc]initWithObjects:搜索,无]forKey:提醒_过期];
[sectionMenus setObject:[[NSArray alloc]initWithObjects:搜索,无]forKey:今天提醒您];
[sectionMenus设置对象:[[NSArray alloc]initWithObjects:搜索,无]forKey:明天提醒];
[sectionMenus setObject:[[NSArray alloc]initWithObjects:搜索,无]forKey:提醒_即将到来];
[sectionMenus setObject:[[NSArray alloc]initWithObjects:搜索,无]forKey:提醒\u];
[SectionMenu设置对象:[[NSArray alloc]initWithObjects:搜索,无]forKey:提醒_完成];
_搜索=否;
}
-(无效)视图显示:(BOOL)动画
{
[超级视图显示:动画];
//每次视图出现时,我们都会检查是否已完成搜索,如果未完成,则执行搜索
如果(!\u已搜索){
_搜索=是;
[自选菜单];
}
}
//tableView委托方法将数组和字典用作数据源
-(NSInteger)表格视图中的节数:(UITableView*)表格视图{
返回[sectionsArray count];
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
返回[[sectionMenus objectForKey:[sectionsArray objectAtIndex:section]]计数];
}
-(NSString*)表格视图:(UITableView*)表格视图标题标题标题部分:(NSInteger)部分{
返回[[sectionsArray对象索引:section]说明];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*simpleTableIdentifier=@“单元格”;
静态NSString*inactiveTableIdentifier=@“InactiveTableItem”;
UITableViewCell*单元格;
NSObject*节点=[[sectionMenus objectForKey:[sectionsArray objectAtIndex:[indexPath section]]]objectAtIndex:[indexPath行]];
单元格=[tableView dequeueReusableCellWithIdentifier:inactiveTableIdentifier];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle重用标识符:不活动标识符];
}
cell.textlab.text=[节点描述];
返回单元;
}
//此方法执行查询以获取每个部分的数据
//并在完成时加载每个部分(理想情况下应在后台完成)
-(void)获取菜单