Pickerview (using parse.com)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Pickerview (using parse.com)

Heidi Anselstetter
Hi,

I have a table "languages" where I have data like "objectId" and "name". This data I want to show in a PickerView.
Fine so far, I put the "name" into an array and show it.

But I want to save the selection back to another table "settings". And there I need to save only the "objectId" of the selected pickerview row into "language".

As far as I can see, the UIPickerView works with arrays, but I can not store the objectId and the name in an array?
I would need to use a dictionary, right?

But a dictionary has no numeric index, so get error messages on functions like

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! { return languagePickerValues[row] } 
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int){ selectedLanguage.text = languagePickerValues[row] self.view.endEditing(true) }

as these functions expect an numeric index.

So what is the right way?
Do I need 1 array and 1 dictionary?
The array for displaying the "name" and the dictionary to match the selected "name" against the dictionary? Don't thinks so, far too dangerous in terms of twin entries...

Does anyone has an idea for me?
I can't be the first person who want to get this working?
But I am searching for 2 days for an solution and can't find any help...

Thanks!

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/8515222c-8424-4a6b-8284-43a83973e6dd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Pickerview (using parse.com)

Heidi Anselstetter
This way it works for me, using an array for the keys and an array for the values:

var gradePickerKeys = [String]()



var gradePickerValues = [String]()

var selectedGradeObjectId: String?


query the data from parse, fill the 2 arrays with keys and values.

var query = PFQuery(className:"Grade")



        query
.orderByAscending("name")

        query
.findObjectsInBackgroundWithBlock {

           
(objects: [AnyObject]!, error: NSError!) -> Void in

           
if error == nil {

               

               
for object in objects {

                   

                   
var name = object["name"] as String

                   

                   
self.gradePickerKeys.append(object.objectId)

                   
self.gradePickerValues.append(name)

                   

                   
self.gradePicker?.reloadAllComponents()

                   

                   
self.gradeTextField.text = self.gradePickerValues.first

                   
self.selectedGradeObjectId = self.gradePickerKeys.first

                   

               
}

           
} else {

               
// Log details of the failure

                println
("\(error.userInfo)")

           
}

 

       
}


// functions needed for picker view



    func numberOfComponentsInPickerView
(pickerView: UIPickerView) -> Int{

       
return 1

   
}

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

       
return gradePickerValues.count

   
}

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

       
return gradePickerValues[row]

   
}

    func pickerView
(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int){

        gradeTextField
.text = gradePickerValues[row]

        selectedGradeObjectId
=  gradePickerKeys[pickerView.selectedRowInComponent(0)] // array value

       
self.view.endEditing(true)

 

   
}


Saving the pointer

let gradeObjectId = PFObject(withoutDataWithClassName: "Grade", objectId: selectedGradeObjectId)



 

var user = PFUser()

 

user
["grade"] = gradeObjectId

 

user
.signUpInBackgroundWithBlock {....


Maybe one day someone can show me a better solution...

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/89d2c525-fef6-47a1-90fe-848518ed153e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Pickerview (using parse.com)

Dennis W
In reply to this post by Heidi Anselstetter
Hi Heidi,

You might consider storing the information for the languages as a dictionary as you mentioned, then you can access the all the values or keys using the standard accessors provided. For example, if you have a dictionary called languages, then you could get the keys or values as an array using the following:

    languages = [1:"English", 2:"Latin", 3:"Spanish", 4:"French"]


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

        var languageNames = Array(languages.values)

        return languageNames[row]


    }


    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

        var languageIds = Array(languages.keys)

        println("item id:\(languageIds[row])")

    }


One issue you might have in this case is ordering, since dictionaries are unordered by nature. You can extract the language names using the above method, sort the resulting array and display the sorted contents in the picker. Then you will need to find the key for a given value (Swift does not have this method, but Objective-C does) using a for-in loop or maybe consider using the NSDictionary type instead of Swift's dictionary. If you think that you might expand the definition of language, such as including country of origin or number of characters, etc, then creating an object to represent the language would be the better way to proceed. Hope this helps.

On Wednesday, February 25, 2015 at 9:15:31 AM UTC-8, Heidi Anselstetter wrote:
Hi,

I have a table "languages" where I have data like "objectId" and "name". This data I want to show in a PickerView.
Fine so far, I put the "name" into an array and show it.

But I want to save the selection back to another table "settings". And there I need to save only the "objectId" of the selected pickerview row into "language".

As far as I can see, the UIPickerView works with arrays, but I can not store the objectId and the name in an array?
I would need to use a dictionary, right?

But a dictionary has no numeric index, so get error messages on functions like

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! { return languagePickerValues[row] } 
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int){ selectedLanguage.text = languagePickerValues[row] self.view.endEditing(true) }

as these functions expect an numeric index.

So what is the right way?
Do I need 1 array and 1 dictionary?
The array for displaying the "name" and the dictionary to match the selected "name" against the dictionary? Don't thinks so, far too dangerous in terms of twin entries...

Does anyone has an idea for me?
I can't be the first person who want to get this working?
But I am searching for 2 days for an solution and can't find any help...

Thanks!

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/7c279efd-40f6-4fb6-9068-fc728efa73ad%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Pickerview (using parse.com)

Brent Royal-Gordon-2

Then you will need to find the key for a given value (Swift does not have this method, but Objective-C does)

 While there's no precise equivalent to -[NSDictionary allKeysForObject:], you can chain filter() and map() to do the same thing:

    map(filter(dict) { _, value in value == 1 }) { key, _ in key }


Or do it lazily in one pass (the above actually uses two loops, one in filter() and the other in map()):

    lazy(dict).filter { _, value in value == 1 }.map { key, _ in key }.array

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/CAEeRk-aE7VCT5MMFFR7CRXU_GhKf7pEhNOpeqXV10DHVnWt5Fw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Pickerview (using parse.com)

Dennis W
In reply to this post by Heidi Anselstetter
Brent, that is great answer, I keep forgetting about Map and Filter in Swift, Thanks!

On Wednesday, February 25, 2015 at 9:15:31 AM UTC-8, Heidi Anselstetter wrote:
Hi,

I have a table "languages" where I have data like "objectId" and "name". This data I want to show in a PickerView.
Fine so far, I put the "name" into an array and show it.

But I want to save the selection back to another table "settings". And there I need to save only the "objectId" of the selected pickerview row into "language".

As far as I can see, the UIPickerView works with arrays, but I can not store the objectId and the name in an array?
I would need to use a dictionary, right?

But a dictionary has no numeric index, so get error messages on functions like

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! { return languagePickerValues[row] } 
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int){ selectedLanguage.text = languagePickerValues[row] self.view.endEditing(true) }

as these functions expect an numeric index.

So what is the right way?
Do I need 1 array and 1 dictionary?
The array for displaying the "name" and the dictionary to match the selected "name" against the dictionary? Don't thinks so, far too dangerous in terms of twin entries...

Does anyone has an idea for me?
I can't be the first person who want to get this working?
But I am searching for 2 days for an solution and can't find any help...

Thanks!

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/ffd96855-9cbd-462a-b547-639ab1920dd8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.