简体   繁体   中英

Swift: How to make a new indexPath for the extra keys of a dictionary

I want the iteration of a dictionary to happen for all keys in it, not just for one in the indexPath.row

struct Whatever {
   var title: String
   var tag: [String:String?]?
}

var cases = [
     Whatever(title: "Name1", tag: ["key1": "value1", "key2":"value2"]),
     Whatever(title: "Name2", tag: ["key3": "value3"]
]

Later in the ViewController:

let arrayCases = cases[indexPath.row]
let caseTag = arrayCases.tag!

for key in caseTag.keys {
    cell.titleLabel?.text = key
    //the magic stops somewhere here

}

for value in caseTag.values {
    if value != nil {
        cell.txt.text = value
    } else {
        cell.txt.text = arrayCases.title
    }
}

Could you tell me how to make a new indexPath.row for the second tag? As if it's a separate insurance of 'Whatever'? Second question - why does it show after each build a different tag - sometimes it's "tag1", other times it's "tag2"? Thank you!

If you use a standard table view (without sections) each item in the data source represents one row. You cannot simply make a new indexPath.row .

You have two options:

  1. Use sections: One Whatever is one section , the title is the header, each tag is one row (see code below)

  2. Concatenate the tag keys and values

    cell.titleLabel?.text = caseTag.keys.joined(separator: ", ") cell.txt.text = caseTag.values.joined(separator: ", ")

Regarding second question : Dictionaries are unordered, there is no order. If you need a specific order use another struct Tag and make tags an array for example

struct Whatever {
    let title: String
    let tags: [Tag]
}

struct Tag {
    let key, value : String
}

let cases = [
    Whatever(title: "Name1", tags: [Tag(key: "key1", value: "value1"), Tag(key: "key2", value: "value2")]),
    Whatever(title: "Name2", tags: [Tag(key: "key3", value: "value3"), Tag(key: "key4", value: "value4")])
]

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    let case = cases[section]
    return case.title
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

...

    let section = cases[indexPath.section]
    let case = section.tags[indexPath.row]
    cell.titleLabel?.text = case.key
    cell.txt.text = case.value

I would add two calculated properties to your struct that returns a list of tag keys and values respectively to make the rest of the code cleaner.

var allTagKeys: String {
    if let keys = tag?.keys {
        return keys.sorted().joined(separator: ", ")
    }
    return ""
}

var allTagValues: String {
    if let values = tag?.compactMap({ $0.value }) {
        return values.joined(separator: ", ")
    }       
    return ""
}

}

Note that I added sorting to the keys, not sure you want that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM