0

i have the dictionary - dictTime: [Int:[Int]] and I'd like to show it in tableView in cell.

To show key in every cell - not a problem, but I'd like to show every element of value of dictionary in "own" UILabel, so I created [UILabel] and understand that count of UILabel in array must be equal count of elements in value of dictionary, but how to do it in func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell for showing for every row (row - it's key-[value])?

7
  • use numberOfRows(inSection section: Int) and return your count. Commented Jan 10, 2017 at 8:41
  • can you show your dictionary and the actual output you want? Commented Jan 10, 2017 at 8:41
  • Who don't you just simply replace cell.textLabel?.text = indexPath.row by cell.textLabel?.text = dictTime[indexPath.row] in cellForRowAt ? Commented Jan 10, 2017 at 8:41
  • @Niharika [1:[1,2,3], 2: [1,2,3]] and so I have two row (because 2 keys) and every row I'd like to have 3 labels at row and first label has text "1", next - "2", third - "3" Commented Jan 10, 2017 at 9:08
  • @the_dahiya_boy i know numbers of rows, because it's count of keys in my dictionary Commented Jan 10, 2017 at 9:12

3 Answers 3

1

Assuming your dictionary is like [Int1: [Int2]], that means:

  • dictTime.allKeys will give you array of all Int1
  • dictTime[Int1] will give you respective [Int2]

Example:

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

For showing these in a 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
    }
Sign up to request clarification or add additional context in comments.

Comments

0

In func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell call an other function and pass your dictionary value to that function for example [1,2,3] also pass your tbaleviewcell to that function. Now in that function run a loop on your dictionary array [1,2,3] and one by one UILabel into your tableviewcell programmatically.

Comments

0

I am not sure I understand the problem. If you want to have as many rows as dictionary entries, use the following:

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

If you want to have as many labels in your cell, as entries for each key in your dictionary, you need to implement a complicated solution.

I would suggest creating a "super cell" which has the maximum amount of labels, put them in a stach view, and hide them according to the number of entries.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.