-
-
Save trungnguyen1791/a479f9e49e8ea0c854b9e54921203ad9 to your computer and use it in GitHub Desktop.
ASCollectionNode show footer only for some sections
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import AsyncDisplayKit | |
class DashboardNode: ASDisplayNode { | |
private let collectionNode: ASCollectionNode | |
init() { | |
let collectionFlow = UICollectionViewFlowLayout() | |
collectionNode = ASCollectionNode(collectionViewLayout: collectionFlow) | |
super.init() | |
automaticallyManagesSubnodes = true | |
collectionNode.delegate = self | |
collectionNode.dataSource = self | |
} | |
override func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec { | |
collectionNode.style.preferredSize = constrainedSize.max | |
return ASAbsoluteLayoutSpec(children: [collectionNode]) | |
} | |
deinit { | |
print("✅ deinit DashboardNode") | |
} | |
} | |
extension DashboardNode: ASCollectionDataSource, ASCollectionDelegate, ASCollectionDelegateFlowLayout { | |
func numberOfSections(in collectionNode: ASCollectionNode) -> Int { | |
return 10 | |
} | |
func collectionNode(_ collectionNode: ASCollectionNode, numberOfItemsInSection section: Int) -> Int { | |
return 10 | |
} | |
func collectionNode(_ collectionNode: ASCollectionNode, nodeBlockForItemAt indexPath: IndexPath) -> ASCellNodeBlock { | |
return { | |
let node = ASTextCellNode() | |
node.text = "\(indexPath.row)" | |
node.backgroundColor = UIColor.lightGray | |
return node | |
} | |
} | |
func collectionNode(_ collectionNode: ASCollectionNode, constrainedSizeForItemAt indexPath: IndexPath) -> ASSizeRange { | |
return ASSizeRangeMake(CGSize(width: collectionNode.calculatedSize.width, height: 44.0)) | |
} | |
func collectionNode(_ collectionNode: ASCollectionNode, supplementaryElementKindsInSection section: Int) -> [String] { | |
return [UICollectionElementKindSectionFooter] | |
} | |
func collectionNode(_ collectionNode: ASCollectionNode, nodeForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> ASCellNode { | |
let node = ASTextCellNode() | |
node.text = "Footer \(indexPath.section)" | |
node.backgroundColor = UIColor.red | |
return node | |
} | |
func collectionNode(_ collectionNode: ASCollectionNode, sizeRangeForFooterInSection section: Int) -> ASSizeRange { | |
if section == 0 { | |
return ASSizeRangeZero | |
} | |
return ASSizeRangeMake(CGSize(width: collectionNode.calculatedSize.width, height: 64.0)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment