2016-01-11 59 views
0

我有一個二維數組的溫度,我想在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" 
    } 

需要注意的是,在計算,打印()語句打印正確的溫度值到控制檯的每一行和每一列的結束。

回答

0

由於我無法評論你的問題在初始階段的聲譽得到澄清,我只會試圖回答根據我對你的問題的理解,並希望以某種方式獲得聲譽,以便我可以在嘗試之前要求澄清未來的答案。 :)

答案嘗試: 在你func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell方法的返回cell.columnLabel.textcell.temperatureLabel.text有條件的基礎上,其中「部分」是你第一次維和「行」與你的兩個維動陣列inedexPath.sectionindexPath.row關聯是你的第二個維度索引。

如果你不介意提供一個2D陣列的例子,我很樂意爲你寫一個樣本。再次,如果這些事情中的任何一個都是可能的,我會親自發表評論或留言給您。只是試圖幫助:)

+0

實際上,如果您要在故事板上添加一些原型單元格並手動輸入想要呈現的內容,請將附加截圖添加到您的OP中,並舉例說明您的2D陣列,這將幫助我們幫助你! :) – dubs