【发布时间】:2014-05-09 05:05:26
【问题描述】:
我正在使用 UICollectionView 和 NSFetchResultsController 来呈现不同的照片集。这是我第一次同时使用 UICollectionView 和 NSFetchResultsController。
这是我的代码:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"CellIdentifier";
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
MyPhoto *myPhoto = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.imageView.image = [UIImage imageWithData:myPhoto.photoData];
return cell;
}
- (NSFetchedResultsController *)fetchedResultsController
{
if (_fetchedResultsController != nil)
{
return _fetchedResultsController;
}
/*
Set up the fetched results controller.
*/
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSManagedObjectContext *moc = [[CoreDataManager sharedInstance] managedObjectContext];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"MyPhoto" inManagedObjectContext:moc];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:40];
// Sort using the timeStamp property.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"sectionName" ascending:YES];
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:YES];
[fetchRequest setSortDescriptors:@[sortDescriptor, sortDescriptor1]];
// Use the folderName property to group into sections.
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:moc sectionNameKeyPath:@"sectionName" cacheName:@"Root"];
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
}
当我有更多部分并尝试滚动视图时,它滚动不顺畅。 我错过了什么吗?
【问题讨论】:
-
为什么您认为是 FRC 导致了问题?你用过时间探查器吗?
-
是的,我使用了时间分析器,如果我深入到 CallTree-> MainThread 等等,但没有得到任何帮助....从我的代码中没有找到任何正在使用的方法更多内存....它显示更多运行时间 -> dispatch_worker_thread2
-
您是否反转了调用树并检查了仅显示 Objective-C?
-
试试这个,打开模拟器并做 Debug->Color Blended Layers。细胞是红色还是绿色?如果它们是红色的,则表示 alpha 层有问题。
-
单元格显示为绿色,但标题-> 标题显示为红色
标签: ios objective-c core-data uicollectionview nsfetchedresultscontroller