首页 文章

在tableview中显示从数组到数组标签的数据

提问于
浏览
0

我有字典 - dictTime: [Int:[Int]] ,我想在单元格 tableView 中显示它 .

要显示每个单元格中的键 - 不是问题,但是我想在"own" UILabel 中显示字典的每个元素值,所以我创建了 [UILabel] 并且理解数组中UILabel的计数必须等于字典值中的元素数,但如何在 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 中显示每一行(行 - 它的键 - [值])?

3 回答

  • 0

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 中调用另一个函数并将字典值传递给该函数,例如 [1,2,3] 也将 tbaleviewcell 传递给该函数 . 现在,在该函数中,在您的字典数组 [1,2,3] 上运行一个循环,并以编程方式逐个 UILabel 运行 UILabel .

  • 0

    假设你的字典就像 [Int1: [Int2]] ,这意味着:

    • dictTime.allKeys将为您提供所有Int1的数组

    • dictTime [Int1]会给你各自的[Int2]

    例:

    var dictTime = [1:[1,2],2:[2,3],3:[3,4]]

    要在tableView中显示这些:

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return dictTime.count
        }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    
            let cell = tableView.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath)
            let keys = Array (dictTime.keys)
            cell.textLabel?.text = String (keys[indexPath.row])
            cell.detailTextLabel?.text = String (dictTime[indexPath.row])
    
            return cell
        }
    
  • 1

    我不确定我是否理解这个问题 . 如果要包含与字典条目一样多的行,请使用以下命令:

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dictTime.count
    }
    

    如果要在单元格中包含尽可能多的标签,作为字典中每个键的条目,则需要实现复杂的解决方案 .

    我建议创建一个“超级单元格”,它具有最大数量的标签,将它们放在一个视图中,并根据条目的数量隐藏它们 .

相关问题