Swift: dictionary access via index

前端 未结 4 1783
不知归路
不知归路 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:22

    Dictionaries are inherently unordered. If you use dictionary.keys, you can get an array of keys and use that, as @Firas says in his/her answer, but there's no guarantee that next time you fetch an array of keys they will be in the same order.

    Another option would be to use Int as the key type, then use the indexPath.row as the key.

    It's really better to use an array as a data source for a table view.

    If you want to store the values for a sectioned table view in a dictionary you should use an outer section array, which contains an inner row array, which contains a dictionary of values for the cell.

提交回复
热议问题