Skip to content

Instantly share code, notes, and snippets.

@ashour
Last active April 9, 2019 08:55
Show Gist options
  • Save ashour/6488e13cb275c96de885d8802b58d5b2 to your computer and use it in GitHub Desktop.
Save ashour/6488e13cb275c96de885d8802b58d5b2 to your computer and use it in GitHub Desktop.
import UIKit
class FeedViewController:
UIViewController,
UITableViewDataSource
{
@IBOutlet weak var feedTableView: UITableView!
fileprivate var productListener: Product.Listener?
fileprivate var products: [Product] = []
{
didSet
{
feedTableView.reloadData()
}
}
override func viewWillAppear(_ animated: Bool)
{
super.viewWillAppear(animated)
productListener = Product.listenToFeed
{
[unowned self] in self.products = $0
}
}
override func viewWillDisappear(_ animated: Bool)
{
super.viewWillDisappear(animated)
productListener?.remove()
}
func tableView(_ tableView: UITableView,
numberOfRowsInSection section: Int)
-> Int
{
return products.count
}
func tableView(_ tableView: UITableView,
cellForRowAt indexPath: IndexPath)
-> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "CELL") as!
FeedTableViewCell
cell.updateUI(with: products[indexPath.row])
return cell
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment