我正在尝试在这里制作我自己的本教程版本(UITableViewCell 内的 UICollectionView):http ://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell
我很难在每个 UITableViewCell 中获得正确的 UICollectionViewCells 计数:
第一个单元格是正确的,它应该在 UICollectionView 内有两个 UICollectionViewCells(当然,在 UITableViewCell 内)。第二个是什么让我失望。每次我在 Core Data 中进行新的保存时,它都会将其添加到以前的单元格中。第二个单元格应该有一个 UICollectionViewCell,而不是两个 UICollectionViewCell。
这是我的代码(AFTableViewCell 和 AFIndexedCollectionView 的代码可以在上面的教程链接中找到):
numberOfItemsInSection:对于嵌入在 UITableViewCell 中的 UICollectionView:
- (NSInteger)collectionView:(AFIndexedCollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
NSIndexPath *indexPath;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Group" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSSortDescriptor *urlDescriptor = [[NSSortDescriptor alloc] initWithKey:@"url" ascending:NO];
NSArray *sortDescriptors = nil;
sortDescriptors = [[NSArray alloc] initWithObjects:urlDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSError *error = nil;
NSArray *fetchedObjects = [[managedObjectContext executeFetchRequest:fetchRequest error:&error]objectAtIndex:indexPath.section];
NSString *temp = [fetchedObjects description];
NSArray *tempArg = [temp componentsSeparatedByString:@","];
return [tempArg count];
}
numberOfSectionsInTableView: 对于主 UITableView:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
id <NSFetchedResultsSectionInfo> sectionInfo = nil;
NSIndexPath *section;
sectionInfo = [[fetchedResultsController sections] objectAtIndex:section.section];
return [sectionInfo numberOfObjects];
}
NSFetchedResultsController:
- (NSFetchedResultsController *)fetchedResultsController
{
TBAppDelegate *delegate = (TBAppDelegate *)[[UIApplication sharedApplication] delegate];
self.managedObjectContext = delegate.managedObjectContext;
NSFetchRequest *fetchRequest = nil;
fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = nil;
entity = [NSEntityDescription entityForName:@"Group" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setFetchBatchSize:INFINITY];
NSSortDescriptor *urlDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];
NSArray *sortDescriptors = nil;
sortDescriptors = [[NSArray alloc] initWithObjects:urlDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSFetchedResultsController *frc = nil;
frc = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[self managedObjectContext] sectionNameKeyPath:nil cacheName:@"Root"];
[frc setDelegate:self];
[self setFetchedResultsController:frc];
return frc;
}
UITableView cellForRowAtIndexpath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CellIdentifier";
AFTableViewCell *cell = (AFTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
{
cell = [[AFTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
return cell;
}
提前致谢。