在uitableviewcell里面使用uicollectionview

我无法解决下一个问题。

我想要显示20个表格单元格,每个单元格包含一个唯一的UICollectionView

1234五

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

函数tableView:cellForRowAtIndexPath:被调用20次,但是collectionView:numberOfItemsInSection:collectionView:cellForItemAtIndexPath:只有4次。 如截图所示, UICollectionViewUICollectionView重复一次。

我的错是什么?

我也面临这个问题,我得到这个问题的原因是因为tableview和collectionView的数据源和委托都是相同的父视图控制器。

它开始为我工作时,我改变了数据源和collectionView的委托给另一个视图说ParentView,我添加到这个ParentView的collectionView,然后最后添加ParentView作为单元的conentView。

我以前的答案指向两个漂亮的教程被删除某人说,我应该包括在这里的内容的基本部分(因为链接可能会在未来失效)…但我不能这样做,因为这些是由教程所有者和整个代码也是必不可less的:),所以现在我给链接的源代码…

Horizo​​ntalCollectionViews

AFTabledCollectionView

-anoop

因为Xcode 7和iOS9可以使用UIStackView – 如果collectionview或tableview是相互包含的最佳解决scheme。