我有一個二維數組的溫度,我想在tableView中顯示。我試圖設置這個tableView與我的數組每行一個部分,並在每個部分,每個tableView行我的數組的一列;也就是說,我要的是:在UITableView中顯示2D陣列
0行0列溫度0,0 第1欄溫度0,1 第2列溫度0,2 等等,等等
行1列0溫度1,0 1柱溫1,1- 第2欄溫度1,2- 等等,等等
行2列0溫度2,0 柱1溫度2,1 第2欄溫度2,2- 等等,等等
我得到的是:
0行0列溫度0,0 第1個欄溫度0,1 第2列溫度0,2 等等,等等
行1列0溫度0 ,0 柱1溫度0,1 第2欄溫度0,2 等等,等等
行2列0溫度0,0 柱1溫度0,1 第2列Temperat .. [溫度ure 0,2 etc. etc.
所有的行(我的tableView中的「sections」)和section0相同。
相關的代碼是:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
for _ in 0..<numberOfRows {
areas.append(columns)
for _ in 0..<numberOfColumns {
columns.append(eachArea)
}
self.tableView.dataSource = self
self.tableView.delegate = self
for var i = 0; i < numberOfRows; i++ {
for j in 0..<numberOfColumns {
let yy = Double(round(100 * temperatures[i][j])/100)
//print("At i = \(i), j = \(j), temperature = \(yy)")
profileArray.append(["Column" : "\(j)", "Temperature" : "\(yy)"])
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//Mark: UITableViewDataSource
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return numberOfRows
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (numberOfColumns)
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let profileDict:Dictionary = profileArray[indexPath.row]
let cell: ProfileCell = tableView.dequeueReusableCellWithIdentifier("myCell") as! ProfileCell
cell.columnLabel.text = profileDict["Column"]
cell.temperatureLabel.text = profileDict["Temperature"]
return cell
}
//Mark: UITableViewDelegate
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 40.0
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "Row \(section) Column Temperature"
}
需要注意的是,在計算,打印()語句打印正確的溫度值到控制檯的每一行和每一列的結束。
實際上,如果您要在故事板上添加一些原型單元格並手動輸入想要呈現的內容,請將附加截圖添加到您的OP中,並舉例說明您的2D陣列,這將幫助我們幫助你! :) – dubs