开发者

NSFetchedResultCintroller with sections+ UITableView + 2 extra rows

I'm trying to add two extra rows to my UITableView. Data comes from the FetchResultsController with sections. I have tried the tricks which normally works with Array but they don't with FetchResultsController with sections. Simply adding +2 in numberofrows doesn't help.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
     return ([[fetchedResultsController sections] count]+2);
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
    return ([sectionInfo numberOfObjects]+2);
}

and the fetchresultcontroller :

- (NSFetchedResultsController *)fetchedResultsController {
    // Set up the fetched results controller if needed.

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

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"eventsEntity" inManagedObjectContext:managedObjectContext];
    [request setEntity:entity];


    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"eventName" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    [request setSortDescriptors:sortDescriptors]; 
    [sortDescriptors release]; 
    [sortDescriptor release];

    NSFetchedResultsController *fetchedResultsController1 =
    [[NSFetchedResultsController alloc] initWithFetchRequest:request
                                        managedObjectContext:managedObjectContext
                                          sectionNameKeyPath:@"eventName" cacheName:nil];


    self.fetchedResultsController = fetchedResultsController1;
    fetchedResultsController.delegate = self;

    [request release];
    [fetchedResultsController1 release];

    retu开发者_Python百科rn fetchedResultsController;
} 


First try to get a graps of the different concepts: sections contain rows.

So if you want to add two rows, you can either add them to an existing section, or add another section and put two rows in that section.

This will probably be the cleanest solution, so here's the deal:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return ([[fetchedResultsController sections] count]+1); // +1 for your section
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    NSArray *sections = [fetchedResultsController sections];

    if ( section < [sections count] )
    {
        // the normal case, e.g. sections 0,1,2 of section.count==3
        id <NSFetchedResultsSectionInfo> sectionInfo = [sections objectAtIndex:section];
        return [sectionInfo numberOfObjects];
    } else {
        // your own section, e.g. the 4th section, where the FRC returned 3 sections
        return 2;
    }
}

Of course, similar amendments are needed in the method that return the cells, titles, row heights, etc etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜