在iOS中遇到UIPickerView问题

时间:2016-10-07 03:55:27

标签: ios swift uipickerview

我有一个UIPickerView的应用,只要选择了某个选择器视图行,它下面的表视图就会更新相关信息。我的问题是,每当应用程序首次打开时,即使默认情况下突出显示/选择了选择器视图的第一行,表视图也是空的。我必须向下滚动,然后返回到第一行,以便它注册为真正选择,并使表视图正确更新。

有没有办法让第一行自动选中,所以我不必每次都向下滚动和备份?我知道那里有一个selectRow()功能,但这并不起作用。它只是做同样的事情。换句话说,行"看起来"选中了,但实际上并没有这样注册。有谁知道这是不是这样,还是有办法绕过它?

编辑:这是我的didSelectRow选择器视图代码:

func pickerView(_ pickerView:UIPickerView, didSelectRow row:Int, inComponent component:Int)
{
    let currentState = abbvsList[statePicker.selectedRow(inComponent: 0)]
    parser = XMLParser(contentsOf:(NSURL(string:"http://alerts.weather.gov/cap/\(currentState).php?x=0") as! URL))!
    alerts = []
    exps = []
    sev = []
    summ = []
    eff = []
    urg = []
    cert = []
    link = []
    latList = []
    longList = []
    parser.delegate = self
    parser.parse()
    alertsTable.reloadData()
    if(alerts.isEmpty) {
        alertsTable.isHidden = true
    }
    else {
        alertsTable.isHidden = false
    }
}

1 个答案:

答案 0 :(得分:0)

加载数据的初始方法,请尝试在viewDidAppearviewDidLoad内调用。

//It should be called after the picker view is loaded.
func loadPickerView(){
    if abbvsList.count > 0 {
        let currentState = abbvsList[0]// get the first element of array
        parser = XMLParser(contentsOf:(NSURL(string:"http://alerts.weather.gov/cap/\(currentState).php?x=0") as! URL))!
        alerts = []
        exps = []
        sev = []
        summ = []
        eff = []
        urg = []
        cert = []
        link = []
        latList = []
        longList = []
        parser.delegate = self
        parser.parse()
        alertsTable.reloadData()
        if(alerts.isEmpty) {
            alertsTable.isHidden = true
        }
        else {
            alertsTable.isHidden = false
        }
    }else{
        //picker view data is empty
    }
}