[英]How to customise Indexpath.row value in iOS swift
我只想檢索表視圖中的某些索引項。 例如:我有:
let array1 = ["one","two","three","four","two","one","three"]
let array2 = ["dog","cat","lion","tiger","elephant","eagle","peacock"]
var duplicatedDict : [String:[Int]] = [:]
for (index,dateString) in array1.enumerated() {
if(duplicatedDict[dateString] == nil){
duplicatedDict[dateString] = [index]
}else{
duplicatedDict[dateString]?.append(index)
}
}
print(duplicatedDict)
//output : ["three": [2, 6], "four": [3], "one": [0, 5], "two": [1, 4]]
當我得到值“ three”時,我必須只獲得array2
2nd和6th(2和6是字符串“ three”在array1中的索引值)索引。
輸出應該在我的UItableView行中(“獅子和孔雀”)。 如何自定義表格視圖以獲取上述輸出?
使用Swift4中的新default
參數可以大大簡化duplicatedDict
Dict的生成:
let array1 = ["one","two","three","four","two","one","three"]
let array2 = ["dog","cat","lion","tiger","elephant","eagle","peacock"]
var duplicatedDict = [String:[Int]]()
for (index, val) in array1.enumerated() {
duplicatedDict[ val, default:[] ].append(index)
}
print(duplicatedDict)
一旦有了它,就可以輕松地從array2
獲取相應的元素:
if let indexes = duplicatedDict["three"] {
let animals = indexes.map { array2[$0] }
print(animals)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.