我已经研究过,我不知道我的代码中缺少什么。我使用了storyboard并将我的BrowseView连接到UICollectionViewController,我已经为我的单元格使用了一个Xib文件,我已经将我的xib文件设置为我的类。
class BrowseView: UICollectionViewController {
let cellIdentifier = "Cell"
override func viewDidLoad() {
super.viewDidLoad()
collectionView?.delegate = self
collectionView?.dataSource = self
let nib = UINib(nibName: "BrowseItemCell", bundle: nil)
collectionView?.register(nib, forCellWithReuseIdentifier: cellIdentifier)
}
}
extension BrowseView {
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as! BrowseItemCell
return cell
}
}
答案 0 :(得分:0)
这是您在代码UICollectionViewFlowLayout
中创建它的方法let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
let collectionView = UICollectionView(frame: frame,
collectionViewLayout: layout)
或者如果CollectionView只是ViewController中应该以这种方式显示的元素之一
let newCollection: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
let collection = UICollectionView(frame: CGRect(x: 0, y: 0, width:0, height: 0), collectionViewLayout: layout)
collection.isScrollEnabled = true
return collection
}()