繁体   English   中英

Swift集合视图与表视图

[英]Swift Collection View With a Table View

我希望在表视图中有一个水平可滚动的集合视图。 此水平集合视图将是表视图中的许多行之一。 这是我原型单元的图片。

在此输入图像描述

以下是我在模拟器中运行它时产生的结果(只显示表视图):

在此输入图像描述

这是我想要它显示的内容:

在此输入图像描述

我不确定这是否是我的代码中的问题,或者它是否可以在故​​事板中修复。

在故事板中创建一个包含UICollectionView (您已经完成)的原型单元格。 设置UIViewControllerdataSource中的UICollectionView ,只是添加了所需的方法UICollectionView

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    //return the array showing number of collectionViewCells in tableViewCell. Of course you will have to handle the data showed in each tableView cell, (maybe using collectionView tags or some other logic)
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

}

您只需要使用UICollectionView和customcell而不是UITableView。 例如,使用以下步骤输出。

1。 为collectionView定义datasourece方法。

2。 根据您的要求,在collectionView的布局委托方法下面放置并自定义

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(2, 2, 2, 2);
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake([UIScreen mainScreen].bounds.size.width/4 - 30, 150);//you need to resize  height and width as per your requirement.
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 10;
}

请查看以下链接。

“UITableViewCell内的UICollectionView”

教程介绍了如何在UITableViewCell中添加UICollectionView(For Horizo​​ntal Scrolling)。

希望这会有所帮助,干杯!!!!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM