设置 UICollectionViewLayer 时,不会调用 cellForItemAt
When UICollectionViewLayer is set, cellForItemAt doesn't called
我想为我的 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 :
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 "bug" 但这解决不了任何问题。
我有没有做对或有特殊性?我迷路了。
我使用 Xcode 8.3 和 Swift 3.1,我的应用程序使用 iOS 10.
您确定您的 ReuseIdentifier 是正确的吗?您是否尝试过它是否进入 if let 此处? (带有打印语句或断点)?
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()
}
不太确定它可能是什么,如果您可以分享您的项目或其中的一部分,我很乐意尝试与您一起解决。另外,请确保您符合 UICollectionViewDataSource
和 UICollectionViewDelegate
.
您是否将委托归于 viewController?这可能是一种可能性。 (如果没有,"numberOsSections" 也不会被调用。
ReuseIdentifier 是正确的。问题是,当我添加 UICollectionViewLayout 时,cellForItemAt 不会调用,当未设置时,UICollectionView 运行 完美。
Without UICollectionViewLayout
With UICollectionViewLayout
有件事我忘了告诉你,UICollectionViewLayout 是否设置并不重要,我的 xibController 进入 "numberOfItemsInSection" 和 "numberOfSections" 只有当 UICollectionViewLayout 设置时,控制器不会通过进入 cellForItemAt
我找到了解决方案,我的自定义 UICollectionViewLayout 没有 contentSize,我在我的 prepare() 函数中添加了它:
let contentWidth = Double(collectionView!.numberOfSections) * CELL_WIDTH
let contentHeight = Double(collectionView!.numberOfItems(inSection: 0)) * CELL_HEIGHT
self.contentSize = CGSize(width: contentWidth, height: contentHeight)
This is the result
我想为我的 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 :
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 "bug" 但这解决不了任何问题。
我有没有做对或有特殊性?我迷路了。
我使用 Xcode 8.3 和 Swift 3.1,我的应用程序使用 iOS 10.
您确定您的 ReuseIdentifier 是正确的吗?您是否尝试过它是否进入 if let 此处? (带有打印语句或断点)?
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()
}
不太确定它可能是什么,如果您可以分享您的项目或其中的一部分,我很乐意尝试与您一起解决。另外,请确保您符合 UICollectionViewDataSource
和 UICollectionViewDelegate
.
您是否将委托归于 viewController?这可能是一种可能性。 (如果没有,"numberOsSections" 也不会被调用。
ReuseIdentifier 是正确的。问题是,当我添加 UICollectionViewLayout 时,cellForItemAt 不会调用,当未设置时,UICollectionView 运行 完美。
Without UICollectionViewLayout
With UICollectionViewLayout
有件事我忘了告诉你,UICollectionViewLayout 是否设置并不重要,我的 xibController 进入 "numberOfItemsInSection" 和 "numberOfSections" 只有当 UICollectionViewLayout 设置时,控制器不会通过进入 cellForItemAt
我找到了解决方案,我的自定义 UICollectionViewLayout 没有 contentSize,我在我的 prepare() 函数中添加了它:
let contentWidth = Double(collectionView!.numberOfSections) * CELL_WIDTH
let contentHeight = Double(collectionView!.numberOfItems(inSection: 0)) * CELL_HEIGHT
self.contentSize = CGSize(width: contentWidth, height: contentHeight)
This is the result