[英]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我希望字典中的所有键都发生迭代,而不仅仅是 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:稍后在 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?你能告诉我如何为第二个标签创建一个新的 indexPath.row 吗? 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"?
第二个问题 - 为什么每次构建不同的标签后都会显示 - 有时是“tag1”,有时是“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 .
您不能简单地创建一个新的 indexPath.row 。
You have two options:您有两个选择:
Use sections: One Whatever
is one section , the title
is the header, each tag
is one row (see code below)使用部分:一个
Whatever
是一个部分, title
是标题,每个tag
是一行(见下面的代码)
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
Tag
并将tags
数组,例如
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.请注意,我向键添加了排序,不确定您是否需要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.