ios - 如何用核心数据实现表部分

标签 ios core-data uitableview

我在我的 iOS 应用程序中使用核心数据。我有一个 tableview 来显示来自名为 persons 的实体的数据。实体人有几个属性,其中一个是“日期”。我想在 TableView 中创建 5 个永久部分(0.今天,1.明天,2.本周,3.本月,4.即将到来),然后我希望数据根据日期。 我一直在寻找这个问题,但没有发现可以应用于我的应用程序。 恳请您告诉我正确的处理方法。

到目前为止,这是我的代码:

#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

最佳答案

您大致需要执行以下操作:

  • 在提取请求的date 属性上添加第一个排序描述符。

  • 向 Person 实体添加一个 transient 属性“sectionIdentifier”, 并实现自定义 getter - (NSString *)sectionIdentifierPerson 托管对象子类,返回 “0”、“1”、“2”、“3”或“4”,具体取决于对象的日期属性。

  • 在创建获取结果 Controller 时设置sectionNameKeyPath:@"sectionIdentifier"

  • 向 TableView Controller 添加一个 titleForHeaderInSection 方法,该方法 返回“今天”、“明天”……取决于部分。

DateSectionTitles Apple 开发者库中的示例项目也演示了其工作原理。

排序描述符将如下所示:

// 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]];

transient “sectionIdentifier”属性的 getter 方法如下所示 (改编自“DateSectionTitles”示例代码):

- (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;
}

要确定日期是否是今天、明天等,您必须使用 NSCalendar 方法。

titleForHeaderInSection 方法与此类似(未经测试,如 这个答案中的其他所有内容):

- (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";
    }
}

关于ios - 如何用核心数据实现表部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20249098/

相关文章:

ios - 分组 uitableview 单元格下的阴影

iphone - 自定义 uitableviewcell 的问题

swift - 我创建的TableViewcontroller.xib应该是初始 Controller

ios - 禁用 SpriteKit 中 SKNode 的旋转

regex - 带有匹配单词开头的字符串的 NSPredicate

objective-c - Restkit - 子映射不继承其父映射?

ios - 多个 NSManagedObjectContexts 或单个上下文和 -performBlock

带有自定义 View Controller 的 iOS 共享扩展

ios - 在应用程序中使用 google maps api 时,GPS 图标不会消失。 iOS

ios - 在 Unity 中为原生 IOS 应用程序提供 subview