swift tableview JSONSerialization 스위프트 테이블뷰 제이슨
//
// ViewController.swift
// WeatherTable
//
// Created by stayfoolish on 21/11/2018.
// Copyright © 2018 stayfoolish. All rights reserved.
//
import UIKit
class ViewController: UIViewController,UITableViewDataSource {
var datalist = NSDictionary()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let baseURL = URL(string: "https://raw.githubusercontent.com/ChoiJinYoung/iphonewithswift2/master/weather.json")
do {
self.datalist = try JSONSerialization.jsonObject(with: Data(contentsOf: baseURL!) , options: JSONSerialization.ReadingOptions.mutableContainers) as! NSDictionary
} catch {
print("Error loading Data")
}
print(self.datalist)
let className = "\(type(of:(((datalist["weatherinfo"] as! NSDictionary)["local"]) as! NSArray).count))"
print("className : \(className)")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return ((datalist["weatherinfo"] as! NSDictionary)["local"] as! NSArray).count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! WeatherCell
//print("indexPath row : \(indexPath.row)")
let dicTemp = ((datalist["weatherinfo"] as! NSDictionary)["local"] as! NSArray)[indexPath.row] as! NSDictionary
print("dicTemp : \(dicTemp)")
cell.countryLabel.text = dicTemp["country"] as? String
let weatherStr = dicTemp["weather"] as? String
cell.weatherLabel.text = weatherStr
cell.temperatureLabel.text = dicTemp["temperature"] as? String
if weatherStr == "맑음" {
cell.imgView.image = UIImage(named: "sunny.png")
}else if weatherStr == "비" {
cell.imgView.image = UIImage(named: "rainy.png")
}else if weatherStr == "흐림" {
cell.imgView.image = UIImage(named: "cloudy.png")
}else if weatherStr == "눈" {
cell.imgView.image = UIImage(named: "snow.png")
}else{
cell.imgView.image = UIImage(named: "blizzard.png")
}
return cell
}
}
//
// WeatherCell.swift
// WeatherTable
//
// Created by stayfoolish on 22/11/2018.
// Copyright © 2018 stayfoolish. All rights reserved.
//
import UIKit
class WeatherCell: UITableViewCell {
@IBOutlet var countryLabel: UILabel!
@IBOutlet var weatherLabel: UILabel!
@IBOutlet var temperatureLabel: UILabel!
@IBOutlet var imgView: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
https://raw.githubusercontent.com/ChoiJinYoung/iphonewithswift2/master/weather.json
{ "weatherinfo": { "local": [ { "country": "한국", "weather": "비", "temperature": "20" }, { "country": "일본", "weather": "맑음", "temperature": "19" }, { "country": "중국", "weather": "눈", "temperature": "14" }, { "country": "스페인", "weather": "우박", "temperature": "13" }, { "country": "미국", "weather": "흐림", "temperature": "2" }, { "country": "영국", "weather": "비", "temperature": "10" }, { "country": "프랑스", "weather": "흐림", "temperature": "15" }, { "country": "브라질", "weather": "흐림", "temperature": "35" }, { "country": "스위스", "weather": "맑음", "temperature": "13" }, { "country": "덴마크", "weather": "비", "temperature": "2" }, { "country": "스웨덴", "weather": "눈", "temperature": "0" }, { "country": "네덜란드", "weather": "비", "temperature": "12" }, { "country": "크로아티아", "weather": "맑음", "temperature": "30" }, { "country": "필리핀", "weather": "맑음", "temperature": "28" }, { "country": "독일", "weather": "눈", "temperature": "3" }, { "country": "헝가리", "weather": "비", "temperature": "13" }, { "country": "벨기에", "weather": "흐림", "temperature": "8" }, { "country": "핀란드", "weather": "우박", "temperature": "15" }, { "country": "이탈리아", "weather": "맑음", "temperature": "23" } ] } }
'Swift > 기초&문법' 카테고리의 다른 글
swift submitvalue present push 스위프트 데이터 전달 (0) | 2018.11.21 |
---|---|
swift alert actionsheet 스위프트 얼럿 액션시트 (0) | 2018.11.20 |
swift tableview xmlparser 스위프트 테이블뷰 xml 파싱 (0) | 2018.11.18 |
swift tableview customcell detail 스위프트 테이블뷰 커스텀셀 디테일 뷰 (0) | 2018.11.17 |
swift tableview 날씨 스위프트 테이블뷰 (0) | 2018.11.16 |