簡體   English   中英

如何在Swift 3中對這個數組進行排序? (選取器視圖)(核心數據)

[英]How can I sort this Array in Swift 3 ? ( Picker View ) ( Core Data )

在此處輸入圖片說明

在此處輸入圖片說明

如何在PickerView中對該數組排序? 我正在使用Swift 3。

無需每次在titleForRow中對數組進行排序, titleForRow需要在初始化stores數組后對其進行一次排序。

stores.sort { $0.name < $1.name }

現在stores數組是按name屬性排序的,現在只需從titleForRow方法返回名稱titleForRow

func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return stores.count
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return stores[row].name
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    print(stores[row].name)
}

Swift 3.1:

var arr = ["aaa","zz","ddd","mnm","zzzz"]
var newArr = arr.sorted()
print(newArr)
//log: ["aaa", "ddd", "mnm", "zz", "zzzz"]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM