6

I can't solve next problem.

I want to display 20 table cells, each contains a unique UICollectionView.

1 2 3 4 5

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSDictionary *package=[_packageList objectAtIndex:indexPath.row];
        static NSString *CellIdentifier = @"package";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        UICollectionView *cellImageCollection=(UICollectionView *)[cell viewWithTag:9];
        cellImageCollection.restorationIdentifier=[NSString stringWithFormat:@"%li", (long)indexPath.row, nil];

        cellTracking.text=[NSString stringWithFormat:@"Row #%li",(long)indexPath.row];
        return cell;
    }


-(NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    int row=[collectionView.restorationIdentifier intValue];
    return [[[_packages objectAtIndex:row] valueForKey:@"imageGallery"] count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    int row=[collectionView.restorationIdentifier intValue];
    NSString *imgStrLink=[[[_packages objectAtIndex:row] valueForKey:@"imageGallery"] objectAtIndex:indexPath.row];
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ImageID" forIndexPath:indexPath];
    UIImageView *imageView=(UIImageView *)[cell viewWithTag:2];
    imageView.image=....;
    return cell;
}

Function tableView:cellForRowAtIndexPath: is called 20 times, but collectionView:numberOfItemsInSection: and collectionView:cellForItemAtIndexPath: only 4 times. As seen in the screenshots UICollectionView is repeated every fourth line.

What is my fault?

2
  • use a custom tableviewcell, make a subclass for it, add your collectionview delegate/datasource to the cell's subclass. I didn't test it my self this way, but I used a collection view cell holding another collection view and it worked. Commented Mar 11, 2014 at 13:13
  • Dont use cell as data source and delegate. Cell should not be responsible for providing such things. Better to subclass collection view and add property named index, then in delegate and data source methods check against index. Use your view controller for collection view delegate and data source. Commented Feb 13, 2015 at 10:22

2 Answers 2

9

I too faced this problem, the reason I got this problem was because both datasource and delegates for the tableview and collectionView were to the same parent viewcontroller.

It started working for me when I changed the datasource and delegate of the collectionView to another View say ParentView, and I added the collectionView to this ParentView and then finally added ParentView as conentView of the cell.

My previous answer which points to two nice tutorials was deleted by someone saying that I should include the essential parts of the content here(because the link may become invalid in future)...but I cannot do that as those are codes written by the tutorial owners and also the whole codes are essential too :), so now I am giving the link to the source code...

HorizontalCollectionViews

AFTabledCollectionView

-anoop

Sign up to request clarification or add additional context in comments.

1 Comment

@anoop4real, bro Could you help my problem stackoverflow.com/questions/44650058/… ?
2

since Xcode 7 and iOS9 you can use UIStackView - best solution for design if collectionview or tableview are containing each other.

2 Comments

It sounds very interesting to use UIStackView. Any code examples please?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.