UITableView中的多个集合视图
我们是否曾经想过需要多个水平可滚动CollectionView的场景? 如果是,则继续。
我们将逐步进行。 让我们开始
- 打开Main.storyboard并添加UIViewController。 在UIViewController中添加UITableView并在tableview内添加tableviewcell
- 在UITableViewCell中添加一个UICollectionView并在其中添加一个UICollectionViewCell
- 将UITableView的约束设置为UIViewController为
topMargin = 0,leftMargin = 0,rightMargin = 0,bottomMargin = 0
4.在UICollectionViewCell中添加一个新图像,在所有上面添加之后,结构将如下图所示
5.接下来,我们必须将CollectionView的可滚动方向设置为水平。
6.将UITableview委托和数据源连接到UIviewController本身。
7.添加UITableViewCell类型的新文件,并将Storyboard中的CollectionView与新添加的文件连接。
类TableViewCell:UITableViewCell,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {@IBOutlet弱var collectionView:UICollectionView!}
8.接下来,在ViewController中,我们将实现UITableView的数据源。 表视图中的numberOfSection将等于所需的collectionview的数量。 在UIViewController中添加以下代码
导入UIKit类ViewController:UIViewController,UITableViewDelegate,UITableViewDataSource {@IBOutlet弱变量tableView:UITableView!部分:Int)->字符串? {返回“ Section Title \(section)”}}重写func didReceiveMemoryWarning(){super.didReceiveMemoryWarning()//处理所有可以重新创建的资源。} func numberOfSections(在tableView中:UITableView)-> Int {return 5} func tableView(_ tableView:UITableView,numberOfRowsInSection部分:Int)-> Int {返回1} func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath)-> UITableViewCell {如果让单元格= tableView.dequeueReusableCell(withIdentifier:“ tableViewCell”,的:indexPath)为? TableViewCell {返回单元格}返回UITableViewCell()}}
9.添加一个类型为UICollectionViewCell的新文件。 在控制面板中,将UICollectionViewCell的类名称更改为新添加的文件。 并使用添加的文件将collctionviewcell故事板上的图像连接起来。
导入UIKitclass CollectionViewCell:UICollectionViewCell {@IBOutlet弱var imageView:UIImageView!override func awakeFromNib(){super.awakeFromNib()}}
10.在TableviewCell文件中,添加以下代码。 在这里,我们正在实现UICollectionViewDataSource。 我们创建了一个包含10张图像的虚拟数组,并将项目数设置为等于数组数。 集合视图中的图像将从数组中添加的图像中显示出来。
//
导入UIKit类TableViewCell:UITableViewCell,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {@IBOutlet弱var collectionView:UICollectionView!var imageArray = [String]()覆盖func awakeFromNib(){super.awakeFromNib()self.collectionView.delegate = selfSource.collectionView。 = selfimageArray = [“ 1.jpeg”,“ 2.jpeg”,“ 3.jpeg”,“ 4.jpeg”,“ 5.jpeg”,“ 6.jpeg”,“ 7.jpeg”,“ 8.jpeg” ”,“ 9.jpeg”,“ 10.jpeg”,“ 1.jpeg”] //初始化代码} func numberOfSections(在collectionView中:UICollectionView)-> Int {返回1} func collectionView(_ collectionView:UICollectionView,numberOfItemsInSection部分:Int)-> Int {return 10} func collectionView(_ collectionView:UICollectionView,cellForItemAt indexPath:IndexPath)-> UICollectionViewCell {如果让单元格:CollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier:“ collectionCell”,用于:indexPath)为? CollectionViewCell {让randomNumber = Int(arc4random_uniform(UInt32(imageArray.count)))cell.imageView.image = UIImage(name:imageArray [randomNumber])return cell} return UICollectionViewCell()} func collectionView(_ collectionView:UICollectionView,layout collectionViewLayout :UICollectionViewLayout,sizeForItemAt indexPath:IndexPath)-> CGSize {let size = CGSize(width:120,height:120)return size}}
在此处下载完整代码