【发布时间】:2016-01-11 18:49:50
【问题描述】:
我有一个二维温度数组,我想在 tableView 中显示。我试图设置这个tableView,每行我的数组有一个部分,并且在每个部分中,每个tableView行我的数组的一列;即,我想要的是:
行 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 列温度 0,2 等等等等。
所有行(我的 tableView 中的“部分”)都与第零部分相同。
相关代码为:
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"
}
请注意,在计算结束时,print() 语句会为每一行和每一列将正确的温度值打印到控制台。
【问题讨论】:
标签: ios arrays swift uitableview