Created
April 11, 2019 16:53
-
-
Save Austin-Griffith/a93ae2ed060d77528f9c51f54aa20e57 to your computer and use it in GitHub Desktop.
custom swift classes for UITableView, UITableViewCell, fetching API data in single file.
This file contains 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
// | |
// DrinkListTableViewController.swift | |
// DrinkupClient | |
// | |
// Created by Austin Griffith on 4/9/19. | |
// Copyright © 2019 Austin Griffith. All rights reserved. | |
// | |
import Foundation | |
import UIKit | |
import Alamofire | |
struct Drink { | |
let id: String | |
let name: String | |
let description: String | |
let amount: Float | |
let image: UIImage | |
init(data: [String: Any]) { | |
self.id = data["id"] as! String | |
self.name = data["name"] as! String | |
//self.amount = data["amount"] as! Float | |
self.amount = ((data["amount"] as? NSNumber)?.floatValue)! | |
self.description = data["description"] as! String | |
self.image = data["image"] as! UIImage | |
} | |
} | |
class DrinkTableViewCell: UITableViewCell { | |
@IBOutlet weak var cellName: UILabel! | |
@IBOutlet weak var cellAmount: UILabel! | |
@IBOutlet weak var cellDescription: UILabel! | |
@IBOutlet weak var cellImage: UIImageView! | |
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String!) { | |
super.init(style: style, reuseIdentifier: reuseIdentifier) | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
} | |
class BeerListTableViewController: UITableViewController { | |
var drinks: [Drink] = [] | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
navigationItem.title = "Beer Selection" | |
tableView.dataSource = self | |
tableView.delegate = self | |
//tableView.register(DrinkTableViewCell.self, forCellReuseIdentifier: "cell") | |
tableView.register(DrinkTableViewCell.self as AnyClass, forCellReuseIdentifier: "cell") | |
//tableView.register(UINib(nibName: "DrinkTableViewCell", bundle: Bundle.main), forCellReuseIdentifier: "cell") | |
//tableView.estimatedRowHeight = 134 | |
//tableView.rowHeight = UITableView.automaticDimension | |
fetchInventory { drinks in | |
guard drinks != nil else { return } | |
self.drinks = drinks! | |
//print("Data from API call: ", self.drinks) | |
//self.tableView.reloadData() | |
// DispatchQueue.main.async { [weak self] in | |
// self?.tableView.reloadData() | |
// } | |
} | |
} | |
override func viewDidAppear(_ animated: Bool) { | |
super.viewDidAppear(animated) | |
DispatchQueue.main.async { [weak self] in | |
self?.tableView.reloadData() | |
} | |
} | |
@IBAction func currentOrderButtonPressed(_ sender: Any) { | |
performSegue(withIdentifier: "orders", sender: nil) | |
} | |
@IBAction func homeButtonPressed(_ sender: Any) { | |
//THIS METHOD WORKS BUT IT IS CREATING A NEW INSTANCE OF THE HOMECONTROLLER SO THE TABBAR GOES AWAY | |
// let homeController = HomeController() | |
// let homeNavigationController = UINavigationController(rootViewController: homeController) | |
// self.present(homeNavigationController, animated: true, completion: nil) | |
let storyboard = UIStoryboard(name: "Main" , bundle: nil) | |
let homeController = storyboard.instantiateViewController(withIdentifier: "homeScreen") | |
let homeNavigationController = UINavigationController(rootViewController: homeController) | |
self.present(homeNavigationController, animated: true, completion: nil) | |
} | |
private func fetchInventory(completion: @escaping ([Drink]?) -> Void) { | |
Alamofire.request("http://127.0.0.1:4000/inventory", method: .get) | |
.validate() | |
.responseJSON { response in | |
guard response.result.isSuccess else { return completion(nil) } | |
guard let rawInventory = response.result.value as? [[String: Any]?] else { return completion(nil) } | |
let inventory = rawInventory.compactMap { pizzaDict -> Drink? in | |
var data = pizzaDict! | |
data["image"] = UIImage(named: pizzaDict!["image"] as! String) | |
//print("Printing each item: ", Drink(data: data)) | |
//printing all inventory successful | |
return Drink(data: data) | |
} | |
completion(inventory) | |
} | |
} | |
// MARK: - Table view data source | |
override func numberOfSections(in tableView: UITableView) -> Int { | |
return 1 | |
} | |
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
print("ROWS: ", drinks.count) | |
return drinks.count | |
} | |
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
//let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! DrinkTableViewCell | |
let cell = UITableViewCell(style: UITableViewCell.CellStyle.subtitle, reuseIdentifier: "cell") | |
//let cell:DrinkTableViewCell = self.tableView.dequeueReusableCell(withIdentifier: "cell") as! DrinkTableViewCell | |
// cell.cellName?.text = drinks[indexPath.row].name | |
// cell.cellAmount?.text = String(drinks[indexPath.row].amount) | |
// cell.cellDescription?.text = drinks[indexPath.row].description | |
// cell.cellImage?.image = drinks[indexPath.row].image | |
cell.imageView?.image = drinks[indexPath.row].image | |
cell.textLabel?.text = drinks[indexPath.row].name | |
cell.detailTextLabel?.text = drinks[indexPath.row].description | |
return cell | |
} | |
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { | |
return 100.0 | |
} | |
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { | |
performSegue(withIdentifier: "pizzaSegue", sender: self.drinks[indexPath.row] as Drink) | |
} | |
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { | |
if segue.identifier == "pizzaSegue" { | |
guard let vc = segue.destination as? PizzaViewController else { return } | |
vc.drink = sender as? Drink | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment