I am trying to use a custom layout attributes in in a custom layout with my collectionview.
I have subclassed UICollectionViewLayoutAttributes and UICollectionViewLayout as shown below
But, layoutAttributesClass() is never called so the layout attributes returned from the call to super.layoutAttributesForItemAtIndexPath [Ln 29] are not of type CustomLayoutAttributes. However, this does work if the layout is a UICollectionViewFlowLayout.
Does anyone have any ideas? I can provide a sample project demonstrating this if it would help.
Thanks.
class CustomLayoutAttributes: UICollectionViewLayoutAttributes {
var displayString: String = "EMPTY"
override func copyWithZone(zone: NSZone) -> AnyObject {
let copy = super.copyWithZone(zone) as! CustomLayoutAttributes
copy.displayString = displayString
return copy
}
override func isEqual(object: AnyObject?) -> Bool {
if let rhs = object as? CustomLayoutAttributes {
if displayString != rhs {
return false
}
return super.isEqual(object)
} else {
return false
}
}
}class CustomLayout: UICollectionViewLayout {
override class func layoutAttributesClass() -> AnyClass {
print("layoutAttributesClass - called")
return CustomLayoutAttributes.self
}
override func prepareLayout() {
super.prepareLayout()
if let collectionView = collectionView {
dataCount = collectionView.numberOfItemsInSection(0)
}
}
override func collectionViewContentSize() -> CGSize {
return collectionView?.frame.size ?? CGSizeZero
}
override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
attributesList = [UICollectionViewLayoutAttributes]()
for i in 0..<dataCount {
if let attributes = layoutAttributesForItemAtIndexPath(NSIndexPath(forItem: i, inSection: 0)) {
attributesList.append(attributes)
}
}
return attributesList
}
override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
if let attributes = super.layoutAttributesForItemAtIndexPath(indexPath) as? CustomLayoutAttributes {
return attributes
}
return nil
}
private var attributesList = [UICollectionViewLayoutAttributes]()
private var dataCount = Int(0)
}