ios 在 uitableviewcell 中使用 uicollectionview

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22326547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:41:36  来源:igfitidea点击:

Use uicollectionview inside uitableviewcell

iosuitableviewuicollectionview

提问by Alex Kondrik

I can't solve next problem.

我无法解决下一个问题。

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

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

12345

12345

-(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 UICollectionViewis repeated every fourth line.

功能tableView:cellForRowAtIndexPath:被称为20倍,但collectionView:numberOfItemsInSection:collectionView:cellForItemAtIndexPath:只有4次。如屏幕截图所示,UICollectionView每四行重复一次。

What is my fault?

我的错是什么?

回答by anoop4real

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.

我也遇到了这个问题,我遇到这个问题的原因是因为 tableview 和 collectionView 的数据源和委托都指向同一个父视图控制器。

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.

当我将 collectionView 的数据源和委托更改为另一个视图(例如 ParentView)时,它开始为我工作,我将 collectionView 添加到此 ParentView,然后最终将 ParentView 添加为单元格的 conentView。

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

AFTabledCollectionView

-anoop

-anoop

回答by kurtanamo

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

从 Xcode 7 和 iOS9 开始,您可以使用 UIStackView - 如果 collectionview 或 tableview 相互包含,则最好的设计解决方案。