昨天已經把 struct 寫好了,今天來呈現資料在手機畫面上。
刻好畫面後,在 ViewController.swift ( MainVC.swift ) 加入程式碼
@IBOutlet weak var backgroundImage: UIImageView!
@IBOutlet weak var locationName: UILabel!
@IBOutlet weak var pop: UILabel!
@IBOutlet weak var maxT: UILabel!
@IBOutlet weak var minT: UILabel!
@IBOutlet weak var wx: UILabel!
@IBOutlet weak var otherCitys: UIButton!
拉完線後程式碼長這樣
p.s.
將所需圖片加入 Assets.xcassets 裡
依據舒適度( wx )搭配適合的 weather Icon
func descriptionToImage () {
if wx.text!.contains("雨") {
if wx.text!.contains("雷") { backgroundImage.image = UIImage(named: "rain1") }
else if wx.text!.contains("晴") { backgroundImage.image = UIImage(named: "rain2") }
else { backgroundImage.image = UIImage(named: "rain3") }
}
else if wx.text!.contains("晴") {
if wx.text!.contains("雲") { backgroundImage.image = UIImage(named: "sunnycloud1") }
else { backgroundImage.image = UIImage(named: "sunnycloud2") }
}
else if wx.text!.contains("陰") { backgroundImage.image = UIImage(named: "cloud") }
else if wx.text!.contains("雲") { backgroundImage.image = UIImage(named: "cloudsunny") }
}
創一個 TableView 畫面,提供使用者選擇城市
在專案底下 New Group / New File / 選 Cocoa Touch Class
宣告城市
struct Citys {
var taiwanCityName: String
}
var cityitem = [
Citys(taiwanCityName: "宜蘭縣"),Citys(taiwanCityName: "花蓮縣"),
Citys(taiwanCityName: "臺東縣"),Citys(taiwanCityName: "澎湖縣"),
Citys(taiwanCityName: "金門縣"),Citys(taiwanCityName: "連江縣"),
Citys(taiwanCityName: "臺北市"),Citys(taiwanCityName: "新北市"),
Citys(taiwanCityName: "桃園市"),Citys(taiwanCityName: "臺中市"),
Citys(taiwanCityName: "臺南市"),Citys(taiwanCityName: "高雄市"),
Citys(taiwanCityName: "基隆市"),Citys(taiwanCityName: "新竹縣"),
Citys(taiwanCityName: "新竹市"),Citys(taiwanCityName: "苗栗縣"),
Citys(taiwanCityName: "彰化縣"),Citys(taiwanCityName: "南投縣"),
Citys(taiwanCityName: "雲林縣"),Citys(taiwanCityName: "嘉義縣"),
Citys(taiwanCityName: "嘉義市"),Citys(taiwanCityName: "屏東縣")
]
TableView Delegate、DataSource
extension TableViewVC: UITableViewDelegate,UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cityitem.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cityCell", for: indexPath) as! TableViewCell
let city = cityitem[indexPath.row]
cell.cityName.text = city.taiwanCityName
return cell
}
func numberOfSections(in tableView: UITableView) -> Int { return 1 }
}
程式碼說明
在的 viewDidLoad() 裡記得加上
cityTableView.delegate = self
cityTableView.dataSource = self
明天會介紹如何辨別點選到哪一個城市,以及將城市天氣資訊呈現在手機上
敬請期待!