Swift: dictionary access via index

前端 未结 4 1769
不知归路
不知归路 2021-02-05 10:54

I want to use values from a dictionary in my UITableViewCells. Can I get those from a dictionary by using indexPath.row for the value and indexPath.section for the key?

4条回答
  •  北荒
    北荒 (楼主)
    2021-02-05 11:20

    you can get the keys array and get the key at indexPath.section like this:

    Array(yourDictionary.keys)[indexPath.section]
    

    and you can get the value at indexPath.row from the values array like this:

    Array(yourDictionary.values)[indexPath.row]
    

    Edit:

    if you want to get the values of a specific section key you should write:

    let key = Array(yourDictionary.keys)[indexPath.section]
    let array = yourDictionary[key]
    let value = array[indexPath.row]
    

提交回复
热议问题