如何在iOS swift中自定义Indexpath.row值

时间:2018-03-23 10:32:37

标签: 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的第2和第6(2和6是array1中字符串“three”的索引值)索引SELECT CONCAT(WEEKOFYEAR(FROM_UNIXTIME(unix_time)), '-', YEAR(FROM_UNIXTIME(unix_time))) as date, AVG(price_usd) AS "model" FROM price_avg INNER JOIN reference ON reference.id_reference=price_avg.id_reference WHERE price_avg.id_reference=1 GROUP BY WEEKOFYEAR(FROM_UNIXTIME(unix_time)), YEAR(FROM_UNIXTIME(unix_time)), price_avg.id_reference ORDER BY unix_time ASC

在我的UItableView行中输出应该是(“lion and peacock”)。如何自定义我的tableview以获得上述输出?

1 个答案:

答案 0 :(得分:0)

使用Swift4中的新duplicatedDict参数可以大大简化default的生成:

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)
}