Skip to content

Instantly share code, notes, and snippets.

@dnaismyth
Created October 22, 2017 02:05
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 dnaismyth/b6379bb49c6eda0570001f552788a5e4 to your computer and use it in GitHub Desktop.
Save dnaismyth/b6379bb49c6eda0570001f552788a5e4 to your computer and use it in GitHub Desktop.
ViewController.swift file for Tutorial # 3
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var gameResults = [Game]()
@IBOutlet var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
setupTableView()
searchForGamesByTitle(title: "mario")
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return gameResults.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let game = gameResults[indexPath.item]
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = game.title
return cell;
}
func setupTableView(){
self.tableView.delegate = self
self.tableView.dataSource = self
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
self.tableView.tableFooterView = UIView() // remove empty cells from tableView
}
func searchForGamesByTitle(title: String){
Request.get(requestUrl: Constants.URL.searchGamesByTitle + title) { (results) in
OperationQueue.main.addOperation { // Get back onto the main thread after our Asynchronous call
for jsonGame: [String: AnyObject] in results {
self.gameResults.append( Game(data: jsonGame) ) // initialize + add game object into our array
}
self.tableView.reloadData()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment