Search code examples
iosswiftcrashsegueuipickerview

UIPickerView.selectRow scrolling make crash


I have 3 UIPickerView, each of them have different values:

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

}

The UIPickerViews are well populated, depending on what values are passed by segue from one ViewController to the one with the PickerViews, the cursor of the pickerViews are at the well place, everything is ok but.. When I try to swap the cursor of any PickerView, the application crashes on the concerned ******PickerView.selectRow(index, inComponent: 0, animated: false). Once the page is charged, I can't change any cursor.

I think it's because if the if context == contextText Indeed, if I set contextText, statusText or fieldText to "", The cursors are set to the first value, but I can change them as I want.

Any idea, trick to avoid these crashes?

Thanks


Solution

  • How about not trying to use PickerView.selectRow inside pickerview:titleForRow... method? That method is being called when the picker view is making the rows ready for display.

    Somewhere after you have setup your picker view with your data source, just loop trough your data and find the index of contentText/statusText/fieldText and (if you find it) call ContextPickerView/StatusPickerView/FieldPickerView.selectRow.