Created
September 25, 2019 08:56
-
-
Save wendyliga/9295025288aeb7446b1d09616afd0890 to your computer and use it in GitHub Desktop.
TextureGram PostNode
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 PostNode: ASTableNode { | |
private let posts: [Post] | |
init(posts: [Post]) { | |
self.posts = posts | |
super.init(style: .plain) | |
self.delegate = self | |
self.dataSource = self | |
self.view.separatorStyle = .none | |
self.style.width = ASDimension(unit: .fraction, value: 1) | |
self.style.height = ASDimension(unit: .fraction, value: 1) | |
self.style.flexShrink = 1 | |
} | |
} | |
extension PostNode: ASTableDataSource, ASTableDelegate{ | |
func numberOfSections(in tableNode: ASTableNode) -> Int { | |
return 1 | |
} | |
func tableNode(_ tableNode: ASTableNode, numberOfRowsInSection section: Int) -> Int { | |
return posts.count | |
} | |
func tableNode(_ tableNode: ASTableNode, nodeBlockForRowAt indexPath: IndexPath) -> ASCellNodeBlock { | |
guard posts.count > indexPath.row else { return { ASCellNode() } } | |
let post = posts[indexPath.row] | |
// this may be executed on a background thread - it is important to make sure it is thread safe | |
let cellNodeBlock = { () -> ASCellNode in | |
return PostCell(post: post) | |
} | |
return cellNodeBlock | |
} | |
func tableNode(_ tableNode: ASTableNode, didSelectRowAt indexPath: IndexPath) { | |
tableNode.deselectRow(at: indexPath, animated: true) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment