M
M
mina_rina2018-10-16 20:59:53
Swift
mina_rina, 2018-10-16 20:59:53

How to display a dictionary in a table?

I have this dictionary:

var sites = defaults.dictionary(forKey: "Sites") as? [String: String] ?? [String: String]()

The values ​​in it are updated every 5 minutes and at the push of a button.
How to display them in a table so that it looks like this:
------------------------
Key1 | value1
-------- -------------
Key2 | value2
-------- -------------
etc.
And what is needed for the table to be updated after the dictionary?
Is there any way to do this via binds?
upd:
Here is a function
func tableView(tableView: NSTableView!, objectValueForTableColumn tableColumn: NSTableColumn!, row: Int) -> NSView? {
       
        if let cell = tableView.makeView(withIdentifier: tableColumn!.identifier, owner: nil) as? NSTableCellView
        {
            
            let domain = [String](sites.keys)
            let status = [String](sites.values)
            if tableColumn?.title == "Sites" {
                cell.text = domain[IndexPath.row]
            } else {
                cell.text = status[IndexPath.row]
            }
            
            return cell;
        }


        return mutableArrayValue[row]
    }

But there is this error:
Value of type 'NSTableCellView' has no member 'text'
opposite cell.text

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mina_rina, 2018-10-18
@mina_rina

Resolved: Dictionary is split into arrays and added to the table with arrays

func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? {
        var domain = [String](sites.keys)
        var status = [String](sites.values)
         if tableColumn?.title == "Sites" {
        return domain[row]
        }
         else{
            return status[row]
        }
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question