ここでこのチュートリアルの独自のバージョンを作成しようとしています (UITableViewCell 内の UICollectionView): http://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell
各 UITableViewCell で UICollectionViewCells の正しい数を取得するのに問題があります。
最初のセルは正しいです。UICollectionView 内に 2 つの UICollectionViewCells が必要です (もちろん、UITableViewCell 内にあります)。2つ目は、私を怒らせているものです。Core Data で新しい保存を行うたびに、以前のセルに追加されます。2 番目のセルには、2 つの UICollectionViewCell ではなく、1 つの UICollectionViewCell が必要です。
これが私のコードです(AFTableViewCellとAFIndexedCollectionViewのコードは上記のチュートリアルリンクにあります):
UITableViewCell に埋め込まれた UICollectionView の numberOfItemsInSection::
- (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];
}
メイン UITableView の numberOfSectionsInTableView::
- (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;
}
前もって感謝します。