Skip to content

Instantly share code, notes, and snippets.

@wendyliga
Created September 25, 2019 08:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wendyliga/9295025288aeb7446b1d09616afd0890 to your computer and use it in GitHub Desktop.
Save wendyliga/9295025288aeb7446b1d09616afd0890 to your computer and use it in GitHub Desktop.
TextureGram PostNode
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