Wire up table cells with loaded JSON

This commit is contained in:
CypherPoet 2019-01-20 23:55:34 -05:00
parent 9d227993b8
commit 810db9b6c4
4 changed files with 74 additions and 43 deletions

View File

@ -8,7 +8,7 @@
/* Begin PBXBuildFile section */
F3FD7F1121F3F56D009FDABA /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3FD7F1021F3F56D009FDABA /* AppDelegate.swift */; };
F3FD7F1321F3F56D009FDABA /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3FD7F1221F3F56D009FDABA /* ViewController.swift */; };
F3FD7F1321F3F56D009FDABA /* HomeViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3FD7F1221F3F56D009FDABA /* HomeViewController.swift */; };
F3FD7F1621F3F56D009FDABA /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F3FD7F1421F3F56D009FDABA /* Main.storyboard */; };
F3FD7F1821F3F56E009FDABA /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = F3FD7F1721F3F56E009FDABA /* Assets.xcassets */; };
F3FD7F1B21F3F56E009FDABA /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F3FD7F1921F3F56E009FDABA /* LaunchScreen.storyboard */; };
@ -18,7 +18,7 @@
/* Begin PBXFileReference section */
F3FD7F0D21F3F56D009FDABA /* Near Earth Objects.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Near Earth Objects.app"; sourceTree = BUILT_PRODUCTS_DIR; };
F3FD7F1021F3F56D009FDABA /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
F3FD7F1221F3F56D009FDABA /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
F3FD7F1221F3F56D009FDABA /* HomeViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeViewController.swift; sourceTree = "<group>"; };
F3FD7F1521F3F56D009FDABA /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
F3FD7F1721F3F56E009FDABA /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
F3FD7F1A21F3F56E009FDABA /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
@ -57,7 +57,7 @@
isa = PBXGroup;
children = (
F3FD7F1021F3F56D009FDABA /* AppDelegate.swift */,
F3FD7F1221F3F56D009FDABA /* ViewController.swift */,
F3FD7F1221F3F56D009FDABA /* HomeViewController.swift */,
F3FD7F2221F420D7009FDABA /* Asteroid.swift */,
F3FD7F1421F3F56D009FDABA /* Main.storyboard */,
F3FD7F1721F3F56E009FDABA /* Assets.xcassets */,
@ -138,7 +138,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
F3FD7F1321F3F56D009FDABA /* ViewController.swift in Sources */,
F3FD7F1321F3F56D009FDABA /* HomeViewController.swift in Sources */,
F3FD7F1121F3F56D009FDABA /* AppDelegate.swift in Sources */,
F3FD7F2321F420D7009FDABA /* Asteroid.swift in Sources */,
);

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14460.31" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14460.31" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="S4o-0H-TVK">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
@ -12,13 +12,13 @@
<!--Home View Controller-->
<scene sceneID="uPH-sy-9I3">
<objects>
<tableViewController id="S4o-0H-TVK" customClass="HomeViewController" customModule="Near_Earth_Objects" customModuleProvider="target" sceneMemberID="viewController">
<tableViewController id="S4o-0H-TVK" customClass="HomeViewController" customModule="Near_Earth_Objects" sceneMemberID="viewController">
<tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="28" sectionFooterHeight="28" id="WIC-fU-OHd">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<prototypes>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" accessoryType="disclosureIndicator" indentationWidth="10" reuseIdentifier="NEO Cell" textLabel="wxr-Gb-aM9" detailTextLabel="ILr-ii-apt" style="IBUITableViewCellStyleSubtitle" id="vss-2x-Dma">
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" accessoryType="disclosureIndicator" indentationWidth="10" reuseIdentifier="Asteroid Cell" textLabel="wxr-Gb-aM9" detailTextLabel="ILr-ii-apt" style="IBUITableViewCellStyleSubtitle" id="vss-2x-Dma">
<rect key="frame" x="0.0" y="28" width="375" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="vss-2x-Dma" id="RBa-9r-viw">
@ -76,9 +76,10 @@
<!--Tab Bar Controller-->
<scene sceneID="N6K-lR-yZX">
<objects>
<tabBarController automaticallyAdjustsScrollViewInsets="NO" id="IaF-o4-zuT" sceneMemberID="viewController">
<tabBarController storyboardIdentifier="TabBarController" automaticallyAdjustsScrollViewInsets="NO" id="IaF-o4-zuT" sceneMemberID="viewController">
<toolbarItems/>
<tabBar key="tabBar" contentMode="scaleToFill" insetsLayoutMarginsFromSafeArea="NO" id="o2y-VY-qVg">
<rect key="frame" x="0.0" y="0.0" width="1000" height="1000"/>
<autoresizingMask key="autoresizingMask"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</tabBar>

View File

@ -0,0 +1,65 @@
//
// ViewController.swift
// Near Earth Objects
//
// Created by Brian Sipple on 1/19/19.
// Copyright © 2019 Brian Sipple. All rights reserved.
//
import UIKit
class HomeViewController: UITableViewController {
var asteroids = [Asteroid]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
loadAsteroids()
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return asteroids.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Asteroid Cell", for: indexPath)
let asteroid = asteroids[indexPath.row]
cell.textLabel?.text = asteroid.name
if let closeApproachDate = asteroid.closeApproachDate {
cell.detailTextLabel?.text = "Close-approach date: \(closeApproachDate)"
} else {
cell.detailTextLabel?.text = ""
}
return cell
}
func loadAsteroids() {
let urlString = "https://api.nasa.gov/neo/rest/v1/neo/browse?api_key=DEMO_KEY"
if let apiURL = URL(string: urlString) {
if let data = try? Data(contentsOf: apiURL) {
parseAsteroidData(data: data)
}
}
}
func parseAsteroidData(data: Data) {
let decoder = JSONDecoder()
if let responseJSON = try? decoder.decode(Asteroids.self, from: data) {
asteroids = responseJSON.nearEarthObjects
print("Fetched \(asteroids.count) asteroids")
tableView.reloadData()
} else {
print("Unable to fetch asteroid JSON")
}
}
}

View File

@ -1,35 +0,0 @@
//
// ViewController.swift
// Near Earth Objects
//
// Created by Brian Sipple on 1/19/19.
// Copyright © 2019 Brian Sipple. All rights reserved.
//
import UIKit
class HomeViewController: UITableViewController {
var nearEarthObjects = [String]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return nearEarthObjects.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "NEO Cell", for: <#T##IndexPath#>)
cell.textLabel?.text = "Title goes here ⚡️"
cell.detailTextLabel?.text = "Subtitle goes here ☄️"
}
}