2
votes

I'm trying to make my own version of this tutorial here (UICollectionView inside UITableViewCell): http://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell

I'm having difficulties getting the correct count of UICollectionViewCells in each UITableViewCell:

enter image description here

The first cell is correct, it should have two UICollectionViewCells inside the UICollectionView (which of course, is inside a UITableViewCell). The second is what is throwing me up. Every time I make a new save in Core Data, it will add it to the previous cells. The second cell should have one UICollectionViewCell, not two UICollectionViewCells.

Here's my code (the code for AFTableViewCell & AFIndexedCollectionView can be found on the above tutorial link):

The numberOfItemsInSection: for the UICollectionView embedded in the UITableViewCell:

- (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];
} 

The numberOfSectionsInTableView: for the main UITableView:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
id <NSFetchedResultsSectionInfo> sectionInfo = nil;

NSIndexPath *section;

sectionInfo = [[fetchedResultsController sections] objectAtIndex:section.section];

return [sectionInfo numberOfObjects]; 
}

The 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;

}

The 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;   
}

Thanks in advance.

1

1 Answers

0
votes

Use

NSArray *fetchedObjects = [[managedObjectContext executeFetchRequest:fetchRequest error:&error]objectAtIndex:section];

instead of

NSArray *fetchedObjects = [[managedObjectContext executeFetchRequest:fetchRequest error:&error]objectAtIndex:indexPath.section];

in your - (NSInteger)collectionView:(AFIndexedCollectionView *)collectionView numberOfItemsInSection:(NSInteger)section function on line 10

It seems the IndexPath local variable is not initialized in this function. You can get section from the parameter passed.

and you haven't initialized section local variable in your

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 

function.

If you do not initialize this variables and use them, they return a garbage value or crashes the program. be careful in these kind of access.