我有3个UIPickerView,它们每个都有不同的值:

let pickerContext = ["Aucun","@office","@computer","@home","@meeting","@read","@achat","@call"]
let pickerStatus = ["LATE","IN-PROCESS","COMPLETED","NOT-STARTED"]
let pickerField = ["INBOX","TODO","WAITING","MAYBE","NOTE"]

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    if(pickerView.isEqual(ContextPickerView)){
        return pickerContext.count
    }
    if(pickerView.isEqual(StatusPickerView)){
        return pickerStatus.count
    }
    if(pickerView.isEqual(FieldPickerView)){
        return pickerField.count
    }
    return 0
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
    if(pickerView.isEqual(ContextPickerView)){
        if contextText != ""{
            println(contextText)
            for (index, context) in enumerate(pickerContext)
            {
                if context == contextText
                {
                    ContextPickerView.selectRow(index, inComponent: 0, animated: false)
                }
            }
        }
        return pickerContext[row]
    }
    if(pickerView.isEqual(StatusPickerView)){
        if statusText != ""{
            println(statusText)
            for (index, status) in enumerate(pickerStatus)
            {
                if status == statusText
                {
                    StatusPickerView.selectRow(index, inComponent: 0, animated: false)
                }
            }
        }
        return pickerStatus[row]
    }
    if(pickerView.isEqual(FieldPickerView)){
        if fieldText != ""{
            println(fieldText)
            for (index, field) in enumerate(pickerField)
            {
                if field == fieldText
                {
                    FieldPickerView.selectRow(index, inComponent: 0, animated: false)
                }
            }
        }
        return pickerField[row]

    }

    return ""
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    if(pickerView.isEqual(ContextPickerView)){
        contextText = pickerContext[row]
    }
    if(pickerView.isEqual(StatusPickerView)){
        statusText = pickerStatus[row]
    }
    if(pickerView.isEqual(FieldPickerView)){
        fieldText = pickerField[row]
    }

}

UIPickerViews填充良好,具体取决于通过segue从一个ViewController传递给带有PickerViews的ViewController传递的值,pickerViews的光标处在适当的位置,一切正常,但是..当我尝试交换任何PickerView,应用程序在有关的******PickerView.selectRow(index, inComponent: 0, animated: false)上崩溃。页面充电完成后,我将无法更改任何光标。

我认为这是因为if context == contextText的确,如果我将contextTextstatusTextfieldText设置为“”,则将光标设置为第一个值,但是我可以根据需要更改它们。

任何想法,避免这些崩溃的窍门?

谢谢

最佳答案

不尝试在pickerview:titleForRow ...方法内使用PickerView.selectRow怎么样?当选择器 View 使行准备好显示时,将调用该方法。

在使用数据源设置选择器 View 之后的某个地方,只需遍历数据并查找contentText / statusText / fieldText的索引,然后(如果找到)调用ContextPickerView / StatusPickerView / FieldPickerView.selectRow。

关于ios - UIPickerView.selectRow滚动使崩溃,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31581156/

10-10 19:13