使用数组填充SwiftyPickerPopover选项

时间:2017-09-28 13:58:23

标签: ios swift3 uiactionsheet

我使用SwiftyPickerPopover显示弹出控件,以便用户选择一个值。以前在应用程序的另一个地方,我已将其实现为:

let displayStringFor:((String?)->String?)? = { string in
            if let s = string {
                switch(s){
                case "Lhe”:
                    return "Lh”
                case "Khi":
                    return "Khi”
                case "Isb”:
                    return "Isb"
                case "Guj":
                    return "Guj"
                default:
                    return s
                }
            }
            return nil
        }

        let p = StringPickerPopover(title: "Select City", choices: ["Lhe”,”Khi”,”Isb”,”Guj”])
            .setDisplayStringFor(displayStringFor)
            .setDoneButton(
                action: {  popover, selectedRow, selectedString in

                    self.cityButton.setTitle(selectedString,for: .normal)
                    if selectedRow == 0 {
                        self.cityImage.image = UIImage(named: "Lhe")
                    } else if selectedRow == 1 {
                        self.cityImage.image = UIImage(named: "Khi")
                    } else if selectedRow == 2 {
                        self.cityImage.image = UIImage(named: "Isb")
                    } else if selectedRow == 3 {
                        self.cityImage.image = UIImage(named: "Guj")
                    }
            })
            .setCancelButton(action: {_, _, _ in
            })
        p.appear(originView: sender as! UIView, baseViewController: self)

您可以在此处看到这些值是硬编码的。现在,我有一个API,可以为我提供所有城市。所以我创建了var mainCitiesArray = [City]()并调用了API并将数据解析为它。

mainCitiesArray我现在想要在此弹出窗口中显示的所有城市。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

您是否只需要将城市名称解析为字符串数组?

E.G。

let citiesNamesArray = mainCitiesArray.map({
    (city: City) -> String in
    return city.name
})

您可以将citiesNamesArray传递给choises参数。