2015-10-17 161 views
0

我正在構建「日期」UiPickerView以便能夠自定義字體大小,類型和顏色。向下滾動時Picker查看崩潰

請參閱我的代碼。

{ 

    @IBOutlet weak var dataInicio: UIPickerView! 

    var dataDia1: NSMutableArray! 
    var dataMes1: NSArray! 
    var dataAno1: NSMutableArray! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 

     self.dataDia1 = NSMutableArray() 

     for dia in 1...31 
     { 
      dataDia1.addObject("\(dia)") 
     } 

     self.dataMes1 = NSArray(objects: "Janeiro","Fevereiro","Março","Abril","Maio","Junho","Julho","Agosto","Setembro","Outubro","Novembro","Dezembro") 

     self.dataAno1 = NSMutableArray() 

     for ano in 1970...2016 
     { 
      dataAno1.addObject("\(ano)") 
     } 


    } 

    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int 
    { 
     return 3 
    } 

    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int 
    { 
     if (component == 0) 
     { 
      return dataDia1.count 
     } 

     if (component == 1) 
     { 
      return dataMes1.count 
     } 

     else 
     { 
      return dataAno1.count 
     } 
    } 

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? 
    { 
     var componentStr = dataDia1[row] 

     if(component == 0) 
     { 
      return componentStr as? String 
     } 

     if(component == 1) 
     { 
      componentStr = (dataMes1[row] as? String)! 
      return componentStr as? String 

     } 
     else 
     { 
      componentStr = (dataAno1[row] as? String)! 
      return componentStr as? String 
     } 
    } 

} 

它的構建成功,但是當我向下滾動一年時,當數組中有超過27個組件時它崩潰。

我開始這個,我已經環顧網絡,並沒有發現任何東西。 ?:(

我在做什麼錯

回答

0

不管你叫var componentStr = dataDia1[row]其中的組件將被要求更改您的代碼如下:。

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? 
{ 
    if(component == 0) 
    { 
     var componentStr = dataDia1[row] 
     return componentStr as? String 
    } 
    else if(component == 1) 
    { 
     var componentStr = (dataMes1[row] as? String)! 
     return componentStr as? String 

    } 
    else 
    { 
     var componentStr = (dataAno1[row] as? String)! 
     return componentStr as? String 
    } 
} 

還要注意使用else if

+0

太棒了!工作得很好,非常感謝您的幫助。 –