uicollectionview计算项目作为部分

时间:2014-03-14 23:45:23

标签: ios iphone core-data uicollectionview

我有一个从核心数据中提取的collectionView。它没有在numberOfItemsInSection中正确计算项目数。它要么只显示一个项目,要么当我实现numberOfSectionsInCollectionView委托时,它将所有项目放在一行而不是网格中。当我强行返回2;我让它正常工作,但当我强迫返回3;我得到了一个崩溃说#0;在索引0和#39;部分的索引2处没有对象。

当我诋毁这段代码时,它只吐出一次4次和4次。

2014-03-14 16:27:30.325 APP[19223:60b] Number of Sections = 4
2014-03-14 16:27:30.326 APP[19223:60b] Number of Items In Section = 1
2014-03-14 16:27:30.326 APP[19223:60b] Number of Items In Section = 1
2014-03-14 16:27:30.327 APP[19223:60b] Number of Items In Section = 1
2014-03-14 16:27:30.327 APP[19223:60b] Number of Items In Section = 1



 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:    (NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections]    [section];
NSLog(@"Number of Items In Section = %lu", (unsigned long)[sectionInfo numberOfObjects]);
return [sectionInfo numberOfObjects];
//    return 3;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
NSLog(@"Number of Sections = %lu", (unsigned long)[[self.fetchedResultsController sections]     count]);
return [[self.fetchedResultsController sections] count];
}

是否有可能将每个项目存储为核心数据中的一个部分?这一切在tableview中都运行良好。

- (NSFetchedResultsController *)fetchedResultsController {

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

    // Create and configure a fetch request with the movie entity.
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Movie" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    // Create the sort descriptors array.
    NSSortDescriptor *titleDescriptor = [[NSSortDescriptor alloc] initWithKey:@"titleString" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:titleDescriptor, nil];
    [fetchRequest setSortDescriptors:sortDescriptors];

    // Create and initialize the fetch results controller.
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"titleString" cacheName:@"Root"];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    NSError *error = nil;
    if (![self.fetchedResultsController performFetch:&error]) {
        // Replace this implementation with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return _fetchedResultsController;
}

0 个答案:

没有答案