【发布时间】:2018-09-30 01:26:58
【问题描述】:
我想在水平方向制作两行集合视图,如下所示
我是iOS开发新手,一些教程只是举例说明如何垂直制作一定数量的collection view,但我需要限制collection view的行数
我尝试将节数设为二,但行数仍为一
import UIKit
class ViewController: UIViewController, UICollectionViewDataSource{
@IBOutlet var collectionView: UICollectionView!
let imageArray = [UIImage(named: "image1"),UIImage(named: "image2"),UIImage(named: "image3"),UIImage(named: "image4"),UIImage(named: "image5"),UIImage(named: "image6"),UIImage(named: "image8"),UIImage(named: "image9")]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 2
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imageArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath) as! ImageCell
cell.avatarView.image = imageArray[indexPath.item]
return cell
}
}
【问题讨论】:
-
集合视图可以是水平的或垂直的。你的是水平的。所以它完全地像你读过的两列垂直集合视图一样工作。在讨论如何使单元格大小合适时,只需将“宽度”替换为“高度”,就可以了。这是微不足道的。不需要部分或其他任何内容。
标签: ios swift uicollectionview