使用自定义布局时未调用 sizeForItemAt 方法 [英] sizeForItemAt method is not calling when using custom layout
问题描述
我正在为我的集合视图使用自定义布局,但如果我使用自定义布局 sizeForItemAt
方法不会调用.
I am using custom layout for my collection view but if i use custom layout sizeForItemAt
method is not calling.
我的自定义布局:
public class HorizontalCollectionViewLayout : UICollectionViewLayout {
var itemSize = CGSize(width: 0, height: 0) {
didSet {
invalidateLayout()
}
}
private var cellCount = 0
private var boundsSize = CGSize(width: 0, height: 0)
public override func prepare() {
cellCount = self.collectionView!.numberOfItems(inSection: 0)
boundsSize = self.collectionView!.bounds.size
}
public override var collectionViewContentSize: CGSize {
let verticalItemsCount = Int(floor(boundsSize.height / itemSize.height))
let horizontalItemsCount = Int(floor(boundsSize.width / itemSize.width))
let itemsPerPage = verticalItemsCount * horizontalItemsCount
let numberOfItems = cellCount
let numberOfPages = Int(ceil(Double(numberOfItems) / Double(itemsPerPage)))
var size = boundsSize
size.width = CGFloat(numberOfPages) * boundsSize.width
return size
}
public override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
var allAttributes = [UICollectionViewLayoutAttributes]()
if cellCount > 0 {
for i in 0...(cellCount-1) {
let indexPath = IndexPath(row: i, section: 0)
let attr = self.computeLayoutAttributesForCellAt(indexPath: indexPath)
allAttributes.append(attr)
}
}
return allAttributes
}
public override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
return computeLayoutAttributesForCellAt(indexPath: indexPath)
}
public override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
return true
}
private func computeLayoutAttributesForCellAt(indexPath:IndexPath)
-> UICollectionViewLayoutAttributes {
let row = indexPath.row
let bounds = self.collectionView!.bounds
let verticalItemsCount = Int(floor(boundsSize.height / itemSize.height))
let horizontalItemsCount = Int(floor(boundsSize.width / itemSize.width))
let itemsPerPage = verticalItemsCount * horizontalItemsCount
let columnPosition = row % horizontalItemsCount
let rowPosition = (row/horizontalItemsCount)%verticalItemsCount
let itemPage = Int(floor(Double(row)/Double(itemsPerPage)))
let attr = UICollectionViewLayoutAttributes(forCellWith: indexPath)
var frame = CGRect(x: 0,y: 0,width: 0,height: 0)
frame.origin.x = CGFloat(itemPage) * bounds.size.width + CGFloat(columnPosition) * itemSize.width
frame.origin.y = CGFloat(rowPosition) * itemSize.height
frame.size = itemSize
attr.frame = frame
return attr
}
}
我在 viewDidLoad
方法中设置此布局:
And i am setting this layout at viewDidLoad
method:
let itemWidth = 100
let itemHeight = 100
let horizontalCV = HorizontalCollectionViewLayout();
horizontalCV.itemSize = CGSize(width: itemWidth, height: itemHeight)
instagramCollection.collectionViewLayout = horizontalCV
self.instagramCollection.delegate=self
self.instagramCollection.dataSource=self
其他集合视图方法工作正常,例如 numberOfItemsInSection
或 cellForItemAt
等.但我无法为集合视图设置插图或单元格间距.
Other collection view methods are working fine like numberOfItemsInSection
or cellForItemAt
etc. But I can't set insets or cell spacing for my collection view.
我尝试了以下代码,以下方法未调用.
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize.init(width: (instagramCollection.frame.width / 3.0), height: instagramCollection.frame.height / 2.0)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 10.0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 10.0
}
我也尝试设置 instets 和故事板的间距也没有奏效.
我的收藏视图现在对所有插图和间距使用零.
My collection view now using zero for all insets and spacing.
如何解决这个问题?
推荐答案
它不会在您使用自己的流程布局时被调用,请按照以下步骤解决:
It won't be called as you are using your own flow layout, as a work around follow these steps:
1- 定义你自己的协议
1- define your own protocol
protocol MyCustomCollectionLayoutDelegate: AnyObject {
func collectionView(_ collectionView: UICollectionView, sizeForItem indexPath: IndexPath) -> CGSize
}
2- 在自定义布局中添加委托属性
2- Add a delegate property inside your custom layout
final class HorizontalCollectionViewLayout: UICollectionViewFlowLayout {
weak var delegate: MyCustomCollectionLayoutDelegate?
....
}
3- 在你的 UICollectionViewController
class MyViewController: UICollectionViewController, MyCustomCollectionLayoutDelegate {
var customLayout: HorizontalCollectionViewLayout? {
let layout = collectionView?.collectionViewLayout as? DeviceDetailsCollectionLayout
layout?.delegate = self
return layout
}
func collectionView(_ collectionView: UICollectionView, sizeForItem indexPath: IndexPath) -> CGSize {
return CGSize(width: 50, height: 60)
}
}
4- 在自定义布局类中调用委托方法
4- call your delegate method inside your custom layout class
public override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let cellIndexPath = IndexPath(item: 0, section: 0)
let customSize = delegate?.collectionView(collectionView, sizeForItem: cellIndexPath)
// use your size ...
}
这篇关于使用自定义布局时未调用 sizeForItemAt 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!