2015-03-08 11 views
5

Ciao, ho cercato di farlo funzionare per anni senza successo, quindi mi scuso se questo sembra facile per voi ragazzi.Utilizzo di UIPickerView con più componenti in swift

Desidero utilizzare un UIPickerView a 3 ruote - La prima ruota ha scelto il valore SelelToRow per modificare l'array per le restanti due ruote ma sarà uguale all'app di conversione.

Sembra darmi un errore quando compongo le ruote dicendo che Anyobject non è identico a String. Non riesco a vedere niente di sbagliato, ma so che è una cosa di base che mi è sfuggita.

Qualsiasi suggerimento sarebbe molto apprezzato.

Motty.

// ViewController.swift 
// picker test 

import UIKit 

class ViewController: UIViewController, UIPickerViewDelegate { 

    @IBOutlet weak var picker1Label: UILabel! 
    @IBOutlet weak var picker2Label: UILabel! 

    @IBOutlet weak var bigPicker: UIPickerView! 

    var wheelContents = [] 
    var length = ["metres","feet","yards","inches","mm","cm","miles"] 
    var volume = ["m3","US Gall","Imp Gall","Barrels", "cubic FT","litres"] 
    var conType = ["length","volume"] 


    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
     wheelContents = [conType,length,length] 

    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 


    //For main selection of type of conversion 
    // returns the number of 'columns' to display. 
    func numberOfComponentsInPickerView(bigPicker: UIPickerView) -> Int{ 

     return wheelContents.count 

    } 

    // returns the # of rows in each component.. 
    func pickerView(bigPicker: UIPickerView, numberOfRowsInComponent component: Int) -> Int{ 

     return wheelContents[component].count 


    } 

    func pickerView(bigPicker: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String!{ 

     return wheelContents[component][row] 

    } 
} 

risposta

1

Dovete dire Swift che l'array wheelContents è un array di array di String:

var wheelContents:[[String]] = [] 

Se non esplicitamente dare wheelContents un tipo, Swift deduce che sia NSArray che è cosa ti sta dando problemi

1

avete bisogno di dire rapida, che il vostro wheelContents è un array di array String:

var wheelContents:[[String]] = [] 

Inoltre si dovrebbe impostare il delegato del pickerView-self perché a gestire il delegato della tua classe. In caso contrario, le funzioni non funzioneranno correttamente:

//In the viewDidLoad method 
bigPicker.delegate = self 
+0

Grazie mille –

1

Dal momento che hai dichiarato wheelContents come questo wheelContents = [], senza specificare il tipo elementi, il compilatore deduce automaticamente che si tratta di serie di AnyObjects aka [AnyObject].

Questo è il motivo per cui, quando si restituisce wheelContents[component].count genera un errore: in quel momento il compilatore si aspetta un String!, ma si stanno fornendo un AnyObject.

È una soluzione davvero semplice, dovresti solo specificare quale sarà il contenuto dell'array quando lo dichiarerai. (Si tratta di un array di array di stringhe aka [[String]])

0

è necessario impostare il DataSource del pickerView insieme al delegato in viewDidLoad

pickerView.dataSource = self