【问题标题】:Display 2D Arrays in a UITableView在 UITableView 中显示 2D 数组
【发布时间】: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


    【解决方案1】:

    由于我无法就您的问题发表评论以在初始级别声誉得到澄清,我将尝试根据我对您问题的理解来回答,并希望以某种方式赢得声誉,以便我可以在尝试之前要求澄清将来回答。 :)

    回答尝试: 在您的func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell 方法中,根据将inedexPath.section 和indexPath.row 与您的二维数组相关联,有条件地返回cell.columnLabel.text 和cell.temperatureLabel.text,其中“section”是您的第一个维度,“row”是您在第二个维度中的索引。

    如果您不介意提供二维数组的示例,我很乐意为您编写示例。同样,如果其中任何一个可能,我会亲自评论或发送消息给您以获取更多详细信息。只是想帮忙:)

    【讨论】:

    • 实际上,如果您要在情节提要上添加一些原型单元格并手动输入您希望它呈现的内容,请将其屏幕截图附加到您的 OP,并给出二维数组的示例这将帮助我们帮助你! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-10
    • 1970-01-01
    • 1970-01-01
    • 2016-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多