如何实现带有核心数据的表部分 [英] How to implement table sections with core data
问题描述
我在我的iOS应用程式中使用核心资料。我有一个tableview来显示来自一个实体的人的数据。实体人有几个属性,其中之一是日期。我想在表视图中创建5个永久段(0.Today,1.Tomorrow,2.This week,3.This月,4.Upcoming),然后我想要的数据显示在每个部分,取决于日期。
我一直在搜索这个问题,但没有发现可以应用到我的应用程序。
我请求你告诉我正确的处理方式。
I am using core data in my iOS app. I have a tableview to show the data from an entity called persons. The entity persons has several attributes, on of them is "date". I would like to create 5 permanent sections in the table view (0.Today, 1.Tomorrow, 2.This week, 3.This month, 4.Upcoming), then I want the data to be shown on each section depending on the date. I have been searching for this question, but nothing found that could be applied to my app. I kindly request you to tell me the right way to handle it.
这是我的代码到目前为止:
This is my code so far:
#import "PersonsTVC.h"
#import "Person.h"
@implementation PersonsTVC
@synthesize fetchedResultsController = __fetchedResultsController;
@synthesize managedObjectContext = __managedObjectContext;
@synthesize selectedPerson;
@synthesize searchResults,titulosseccion;
- (void)setupFetchedResultsController
{
// 1 - Decide what Entity you want
NSString *entityName = @"Person"; // Put your entity name here
NSLog(@"Setting up a Fetched Results Controller for the Entity named %@", entityName);
// 2 - Request that Entity
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:entityName];
// 3 - Filter it if you want
//request.predicate = [NSPredicate predicateWithFormat:@"Person.name = Blah"];
// 4 - Sort it if you want
request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"firstname"
ascending:YES
selector:@selector(localizedCaseInsensitiveCompare:)]];
// 5 - Fetch it
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:self.managedObjectContext
sectionNameKeyPath:nil
cacheName:nil];
[self performFetch];
}
- (void) viewDidLoad
{
self.searchResults = [NSMutableArray arrayWithCapacity:[[self.fetchedResultsController fetchedObjects] count]];
[self.tableView reloadData];
}
-(void) viewDidUnload{
self.searchResults = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self setupFetchedResultsController];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [[self.fetchedResultsController sections] count];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Perform segue to detail when a SEARCH table cell is touched
if(tableView == self.searchDisplayController.searchResultsTableView)
{
[self performSegueWithIdentifier:@"Person Detail Segue" sender:tableView];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Persons Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
// Configure the cell...
Person *person = nil;
if (tableView == self.searchDisplayController.searchResultsTableView)
{
NSLog(@"Configuring cell to show search results");
person = [self.searchResults objectAtIndex:indexPath.row];
}
else
{
NSLog(@"Configuring cell to show normal data");
person = [self.fetchedResultsController objectAtIndexPath:indexPath];
}
NSString *fullname = [NSString stringWithFormat:@"%@ %@", person.firstname, person.surname];
cell.textLabel.text = person.firstname;
if ([person.inRole.color isEqual :@"Yellow"])
{
cell.imageView.image = [UIImage imageNamed:@"Yellow"];
}
if ([person.inRole.color isEqual :@"Black"])
{
cell.imageView.image = [UIImage imageNamed:@"Black"];
}
if ([person.inRole.color isEqual :@"Grey"])
{
cell.imageView.image = [UIImage imageNamed:@"Grey"];
}
if ([person.inRole.color isEqual :@"Red"])
{
cell.imageView.image = [UIImage imageNamed:@"Red"];
}
if ([person.inRole.color isEqual :@"Blue"])
{
cell.imageView.image = [UIImage imageNamed:@"Blue"];
}
if ([person.inRole.color isEqual :@"Dark Green"])
{
cell.imageView.image = [UIImage imageNamed:@"DarkGreen"];
}
if ([person.inRole.color isEqual :@"Light Green"])
{
cell.imageView.image = [UIImage imageNamed:@"LightGreen"];
}
if ([person.inRole.color isEqual :@"Light Blue"])
{
cell.imageView.image = [UIImage imageNamed:@"LightBlue"];
}
if ([person.inRole.color isEqual :@"Brown"])
{
cell.imageView.image = [UIImage imageNamed:@"Brown"];
}
if ([person.inRole.color isEqual :@"Dark Orange"])
{
cell.imageView.image = [UIImage imageNamed:@"DarkOrange"];
}
NSDate *fechasinformat = person.date;
NSString *fecha0 = [NSString stringWithFormat:@"%@", fechasinformat];
cell.detailTextLabel.text = fecha0;
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView)
{
return [self.searchResults count];
}
else
{
return [[[self.fetchedResultsController sections] objectAtIndex:section] numberOfObjects];
}
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.tableView beginUpdates]; // Avoid NSInternalInconsistencyException
// Delete the person object that was swiped
Person *personToDelete = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSLog(@"Deleting (%@)", personToDelete.firstname);
[self.managedObjectContext deleteObject:personToDelete];
[self.managedObjectContext save:nil];
// Delete the (now empty) row on the table
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self performFetch];
[self.tableView endUpdates];
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"Add Person Segue"])
{
NSLog(@"Setting PersonsTVC as a delegate of PersonDetailTVC");
PersonDetailTVC *personDetailTVC = segue.destinationViewController;
personDetailTVC.delegate = self;
NSLog(@"Creating a new person and passing it to PersonDetailTVC");
Person *newPerson = [NSEntityDescription insertNewObjectForEntityForName:@"Person"
inManagedObjectContext:self.managedObjectContext];
personDetailTVC.person = newPerson;
}
else if ([segue.identifier isEqualToString:@"Person Detail Segue"])
{
NSLog(@"Setting PersonsTVC as a delegate of PersonDetailTVC");
PersonDetailTVC *personDetailTVC = segue.destinationViewController;
personDetailTVC.delegate = self;
// Store selected Person in selectedPerson property
if(sender == self.searchDisplayController.searchResultsTableView)
{
NSIndexPath *indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
self.selectedPerson = [self.searchResults objectAtIndex:[indexPath row]];
}
else
{
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
self.selectedPerson = [self.fetchedResultsController objectAtIndexPath:indexPath];
}
NSLog(@"Passing selected person (%@) to PersonDetailTVC", self.selectedPerson.firstname);
personDetailTVC.person = self.selectedPerson;
}
else
{
NSLog(@"Unidentified Segue Attempted!");
}
}
- (void)theSaveButtonOnThePersonDetailTVCWasTapped:(PersonDetailTVC *)controller
{
// do something here like refreshing the table or whatever
// close the delegated view
[controller.navigationController popViewControllerAnimated:YES];
}
#pragma mark -
#pragma mark Content Filtering
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
self.searchResults = [[self.fetchedResultsController fetchedObjects] filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
Person* person = evaluatedObject;
NSString* firstName = person.firstname;
//searchText having length < 3 should not be considered
if (!!searchText && [searchText length] < 3) {
return YES;
}
if ([scope isEqualToString:@"All"] || [firstName isEqualToString:scope]) {
return ([firstName rangeOfString:searchText].location != NSNotFound);
}
return NO; //if nothing matches
}]];
}
#pragma mark -
#pragma mark UISearchDisplayController Delegate Methods
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString scope:@"All"];
return YES;
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
[self filterContentForSearchText:[self.searchDisplayController.searchBar text] scope:@"All"];
return YES;
}
@end
推荐答案
您大致可以执行以下操作:
You would have roughly to do the following:
-
添加第一个排序
Add a first sort descriptor on the
date
attribute to the fetch request.
添加暂态 >属性sectionIdentifier到Person实体,
并实现自定义getter - (NSString *)sectionIdentifier
到 Person
托管对象子类,它返回
0,1,2,3或4,取决于 date
属性。
Add a transient property "sectionIdentifier" to the Person entity,
and implement a custom getter - (NSString *)sectionIdentifier
to the Person
managed object subclass, that returns
"0", "1", "2", "3", or "4", depending on the date
attribute of the object.
设置 sectionNameKeyPath:@sectionIdentifier
向表视图控制器添加 titleForHeaderInSection
方法,
Add a titleForHeaderInSection
method to the table view controller, that
returns "Today", "Tomorrow", ... depending on the section.
来自Apple Developer Library的href =http://developer.apple.com/library/ios/#samplecode/DateSectionTitles/Introduction/Intro.html =nofollow> DateSectionTitles 示例项目还演示了如何
The DateSectionTitles sample project from the Apple Developer Library also demonstrates how this works.
然后,排序描述符将如下所示:
The sort descriptors would then look like this:
// First sort descriptor (required for grouping into sections):
NSSortDescriptor *sortByDate = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:YES];
// Second sort descriptor (for the items within each section):
NSSortDescriptor *sortByName = [[NSSortDescriptor alloc] initWithKey:@"firstname" ascending:YES];
[request setSortDescriptors:@[sortByDate, sortByName]];
瞬态sectionIdentifier属性的getter方法看起来像
DateSectionTitles示例代码):
The getter method for the transient "sectionIdentifier" property would look like (adapted from the "DateSectionTitles" sample code):
- (NSString *)sectionIdentifier
{
[self willAccessValueForKey:@"sectionIdentifier"];
NSString *tmp = [self primitiveValueForKey:@"sectionIdentifier"];
[self didAccessValueForKey:@"sectionIdentifier"];
if (!tmp)
{
NSDate *date = self.date;
// Using pseudo-code here:
if ("date is from today") {
tmp = @"0";
} else if ("date is from tomorrow") {
tmp = @"1";
} else ... // and so on ...
[self setPrimitiveValue:tmp forKey:@"sectionIdentifier"];
}
return tmp;
}
要确定日期是否在今天,明天等, code> NSCalendar
方法。
To determine if the date falls on today, tomorrow etc you have to use NSCalendar
methods.
titleForHeaderInSection
方法将类似于这个(未测试,作为
的一切在这个答案):
The titleForHeaderInSection
method would be similar to this (untested, as
everything else in this answer):
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> theSection = [[self.fetchedResultsController sections] objectAtIndex:section];
NSString *sectionName = [theSection name];
if ([sectionName isEqualToString:@"0"]) {
return @"Today";
} else if ([sectionName isEqualToString:@"1"]) {
return @"Tomorrow";
} ... // and so on ...
} else {
return @"Other";
}
}
这篇关于如何实现带有核心数据的表部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!