我有一堆数组:
let CNSNRT = ["CNS", "NRT", 3637, 2113] as [Any]
let ADKANC = ["ADK", "ANC", 1192, 2200] as [Any]
let LAXMCE = ["LAX", "MCE", 259 , 2370] as [Any]
let ANCMCG = ["ANC", "MCG", 219, 2440] as [Any]
let ATLGLH = ["ATL", "GLH", 378, 2590] as [Any]
let CNSHKG = ["CNS", "HKG", 3450, 2728] as [Any]
let AKPFAI = ["AKP", "FAI", 253, 2960] as [Any]
let ADLKUL = ["ADL", "KUL", 3524, 2984] as [Any]
合并它们:
let routesArray = [CNSNRT, ADKANC, LAXMCE, ANCMCG, ATLGLH, CNSHKG, AKPFAI, ADLKUL]
使用
print(routesArray[0])
,我得到了["CNS", "NRT", 3637, 2113]
,而使用print(routesArray[0][0])
,我得到了CNS
,所以一切似乎引用正确。但是,我不知道如何将其正确地放入
UITableView
中。基本上,routesArray
中的每个项目都应该是一个单元格,而数组中的项目将定义单元格中的标签。当前代码:
import UIKit
class ViewController: UIViewController, UITableViewDataSource {
@IBOutlet weak var routesTableView: UITableView!
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return routesArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = routesTableView.dequeueReusableCell(withIdentifier: "cellReuseIdentifier")!
let text = "\(routesArray[indexPath.row])"
cell.textLabel?.text = text
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
routesTableView.dataSource = self
}
生成一个表格,其中每一行看起来像
["CNS", "NRT", 3637, 2113]
,但我无法弄清楚如何将它们分解以将其应用于单元格上的特定标签。因此,例如,originLabel
应该显示 CNS
,但在下一个单元格中,originLabel
应该显示 ADK
。