【发布时间】:2017-04-14 21:15:14
【问题描述】:
我想为我的 UICollectionView 设置 UICollectionViewLayout,但是在设置图层时,不会调用函数 cellForItemAt。我不明白为什么。 我的 UICollectionView 位于 XIB 中,此代码用于测试 UICollectionViewLayout :
required init?(coder aDecoder: NSCoder)
{
super.init(coder: aDecoder)
xibSetup()
self.collectionView.register(UINib(nibName: "StepCell", bundle: nil), forCellWithReuseIdentifier: stepCellIdentifier)
self.collectionView.delegate = self
self.collectionView.dataSource = self
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 3
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 100
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let lCell = collectionView.dequeueReusableCell(withReuseIdentifier: stepCellIdentifier, for: indexPath) as? StepCell {
lCell.text.text = indexPath.row.description
return lCell
}
return UICollectionViewCell()
}
我在另一个 xib 中有一个自定义 UICollectionViewCell,这是我的 UICollectionViewLayout 类:
class DetailGroupCollectionviewLayout: UICollectionViewLayout {
let CELL_HEIGHT = 30.0
let CELL_WIDTH = 100.0
var cellAttrsDictionary = Dictionary<IndexPath,UICollectionViewLayoutAttributes>()
var contentSize = CGSize.zero
override var collectionViewContentSize: CGSize {
return self.contentSize
}
override func prepare() {
if let lCollectionView = self.collectionView {
if lCollectionView.numberOfSections > 0 {
for section in 0...lCollectionView.numberOfSections - 1 {
if lCollectionView.numberOfItems(inSection: section) > 0 {
for item in 0...lCollectionView.numberOfItems(inSection: section) - 1 {
let cellIndex = IndexPath(item: item, section: section)
let xPos = Double(item) * CELL_WIDTH
let yPos = Double(section) * CELL_HEIGHT
let cellAttributes = UICollectionViewLayoutAttributes(forCellWith: cellIndex)
cellAttributes.frame = CGRect(x: xPos, y: yPos, width: CELL_WIDTH, height: CELL_HEIGHT)
cellAttributes.zIndex = 1
cellAttrsDictionary[cellIndex] = cellAttributes
}
}
}
}
}
}
}
我试图在故事板中包含有关 UICollectionView 的所有内容,因为我认为这是一个 xib“错误”,但这没有解决任何问题。
有什么东西我做的不正确或有什么特殊性?我迷路了。
我在 Xcode 8.3 和 Swift 3.1 上,我的应用在 iOS 10 上。
【问题讨论】:
标签: swift uicollectionview uicollectionviewcell uicollectionviewlayout