UICollectionView with Multiple subclassed UICollectionViewCell

匆匆过客 提交于 2019-12-04 02:38:50

It appears there is an issue with the sorting which needs to be sorted.

But my question is why are you switching on the section name? Why not just switch on the class of the item?

id object = [self.fetchedResultsController objectAtIndexPath:indexPath];

if ([object isKindOfClass:[DatasetA class]] {
  // Do your DatasetCellA stuff
} else if ([object isKindOfClass:[DatasetB class]) {
  // Do your DatasetCellB stuff
}

At least this way you don't have issues with the sorting

You probably want to correct your fetch request so that you sort by datasetType and then by name.

- (NSFetchedResultsController *)fetchedResultsController
{
  if (!_fetchedResultsController) {
    return _fetchedResultsController;
  }

  NSFetchRequest *request = [[NSFetchRequest alloc] init];
  request.entity          = [Dataset MR_entityDescription];
  request.sortDescriptors = @[
                            [NSSortDescriptor sortDescriptorWithKey:@"datasetType" ascending:YES],
                            [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:NO],
                            ];

  _fetchedResultsController = 
    [[NSFetchedResultsController alloc] initWithFetchRequest:request
                                        managedObjectContext:[NSManagedObjectContext MR_context]
                                          sectionNameKeyPath:@"datasetType"
                                                   cacheName:nil];

  NSError *error = nil;
  if (![_fetchedResultsController performFetch:&error]) {
    NSLog(@"Could not perform fetch %@", [error localizedDescription]);
  }

  _fetchedResultsController.delegate = <..your delegate..>;

  return _fetchedResultsController;
}

Also the reason your self made fetchResultsController probably returned nothing is because you don't perform the fetch with the fetch request yourself, you do it through the fetchedResultsController like

if ([fetchedResultsController performFetch:&error]) {
   //...

Without more knowledge of the model structure I can't be more certain, but here goes...

I think your problem is that you expect the search results to be sorted a certain way, but they're not sorted in the way you expect them.

The dataset is using a groupBy:@"datasetType" clause, which defines the sections. But the sections will also be sorted implicitly by the groupBy key. So the question is:

What is this datasetType key in your model, and is it what you expect it to be? Clearly you want to sort by dataset name, but is datasetType what the name is? This is where I believe the problem lies. You're retrieving the wrong sort order because you're not grouping by the dataset's name.

PS: you have "DATSET A" and "DATASET B"... So if you are indeed grouping by the dataset name, it is all working as expected... except for your names which have typos.

You apparently have a typo in your dataset names:

2013-04-04 10:59:38.704 [2380:14003] section name is DATSET A

With the second 'A' missing in DATSET A, this will sort after DATASET B.

The hazards of literals.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!