Stephy Samaniego
Stephy Samaniego

Reputation: 242

UIPickerView not showing the items

I'm almost new in Swift and I'm having troubles with the UIPickerView. The problem is this:

When I put items in the picker I do it successfully and I can choose. Everything is perfect but I don't know why when I have like one or two items they don't show up, and I have to drag it down some times to see it, and it is selected but the problem is I can't see it :(

This is my code:

 class LoginViewController: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate{

@IBOutlet var pickerCol: UIPickerView!

@IBOutlet var labelCol: UILabel!

@IBOutlet var textUsuario: UITextField!
@IBOutlet var textPassword: UITextField!

let defaults = NSUserDefaults.standardUserDefaults()
var picker = UIPickerView()
var NumberOfRows = 0
var NamesArray = [String]()
var IdArray = [String]()    
var numero = 0
var col : String = ""

override func viewDidLoad() {
    pickerCol.delegate = self
    pickerCol.dataSource = self
    parseJSON()
    super.viewDidLoad()       
}

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

func parseJSON(){
    let parameters = [
        "opcion": "listar_clientes",
        "opci_codi" : "320"
    ]

    Alamofire.request(.POST, "http://desarrollo.educalinks.com.ec/mobile/main.php", parameters: parameters).responseJSON { response in
        let JSON1 = (response.result.value)
        let result = JSON1!["result"] as! NSArray
        let dataExample : NSData = NSKeyedArchiver.archivedDataWithRootObject(JSON1!)            
        NSLog("\(JSON1)")
        NSLog("\(result.count)")

        for i in 0..<result.count{
            let id = result[i]["id"] as AnyObject? as? Int
            let texto = result[i]["texto"] as AnyObject? as? String
            var idcole = String(id)
            self.NamesArray.append(texto!)
            self.IdArray.append(idcole)
            self.NumberOfRows = self.NamesArray.count
        }
    }
}

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

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
    return NumberOfRows
}

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    labelCol.text=NamesArray[row]
    col = IdArray[row]
}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    if(NamesArray.count != 0){
        return NamesArray[row]
    }
    return nil
}

I have been hours trying to figure out why is this happening. Before this I had the picker with 10 items and it appears normally. But with two items no. I appreciate the help.

Upvotes: 2

Views: 3052

Answers (1)

ChikabuZ
ChikabuZ

Reputation: 10185

You should call pickerCol.reloadAllComponents() after for loop.

Upvotes: 3

Related Questions