2017-01-10 62 views
0

我有字典 - dictTime: [Int:[Int]],我想在单元格中显示tableView在tableview中显示从数组到数组标签的数据

要显示在每个细胞中的关键 - 不是一个问题,但我想显示的词典的价值每一个元素中的“自己” UILabel,所以我创建[UILabel]和理解的UILabel的数组数必须相等数字典值的元素,但如何在func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell中显示每行(行 - 它是关键 - [值])?

+0

使用'numberOfRows(切入口段中:int)'和回报您的计数。 –

+0

你可以显示你的字典和你想要的实际输出吗? – Niharika

+0

谁不只是简单地用'cellForRowAt'中的'cell.textLabel?.text = dictTime [indexPath.row]'替换'cell.textLabel?.text = indexPath.row'? – Toldy

回答

0

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

0

我不确定我是否理解这个问题。如果你想拥有尽可能多的行作为字典条目,使用以下命令:如果你想在你的多张标签,因为在你的字典中的每个键条目

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

,你需要实现一个复杂的解。

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

1

假设你的词典就像是[INT1:[INT2],这意味着:

  • dictTime.allKeys会给你所有INT1
  • dictTime [INT1]数组会给你相应的[ INT2]

实施例:

变种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 
    } 
相关问题