Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selection from the autocomplete always returns the first value and not the actual selected value #23

Open
sysadminPSQ opened this issue Nov 7, 2015 · 15 comments

Comments

@sysadminPSQ
Copy link

Is there a way to return the tableview selected index value?

@mirzadelic
Copy link

I have this problem too, did you solve it?

@sysadminPSQ
Copy link
Author

@mirzadelic

Modifying the plugin was a bit tedious.

I found a much better and simple way of doing this. Please refer to the link below. Just copy and paste the answer in the comments and it works like a charm.

http://stackoverflow.com/questions/26885438/getting-autocomplete-to-work-in-swift

Hope this helps.

@mirzadelic
Copy link

Yes, but i need it as dropdown :(

@sysadminPSQ
Copy link
Author

I am attaching a screenshot of how it appears.

It's very similar to this plugin. Hope it helps.

screen shot 2015-11-20 at 12 33 59 am

@mirzadelic
Copy link

Is this table that hide and show?
I used this code http://stackoverflow.com/a/32948918/1204527
But tableview doesn't show?

@mirzadelic
Copy link

Do i need to add tableview in storyboard?

@sysadminPSQ
Copy link
Author

Yes it's a tableview that shows and hides.

Yes add it in the storyboard and set the datasource and delegate and it should work!

@mirzadelic
Copy link

Ohh, i tried without adding tableview to storyboard..
Thanks..

@sysadminPSQ
Copy link
Author

Also link the tableview to an IBOutlet if you want it to dismiss when the view ends editing.

@mirzadelic
Copy link

How to hide tableveiw when done editing?

@sysadminPSQ
Copy link
Author

Create an IBOutlet for the tableview and then in the didSelectRowAtIndexPath function make the changes below

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let selectedCell : UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
    textField.text = selectedCell.textLabel!.text
    tableview.hidden = true     
} 

@mirzadelic
Copy link

I did that, but i now i have problem when i touch outsite textfield, to hide tableview and close keyboard?

@sysadminPSQ
Copy link
Author

Simple. Add this code in your view controller

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

        self.view.endEditing(true)
        tableView.hidden = true
}

@mirzadelic
Copy link

Thanks, it works great..

@cabotmoose
Copy link

Couple of tweaks to make it work with Swift 2.0 in case anyone else reads this. Add a class var called "selectedText", and make these three changes:

At the beginning of layoutSubviews, clear out the selectedText class var:

override func layoutSubviews(){

        super.layoutSubviews()
        let str : String = self.text!

        if (str.characters.count > 0) && (self.isFirstResponder())
        {
            if (mDelegate != nil){
                self.selectedText = nil

Set selectedText in the didSelectRowAtIndexPath func before you resign first responder:

self.selectedText = self.applyFilterWithSearchQuery(self.text!)[indexPath.row]["DisplayText"] as? String

Finally update the handleExit method:

//Swift 2.0 compatible
func handleExit(){
        ...
        if ((mDelegate?.textFieldShouldSelect?(self)) != nil){
            if (self.applyFilterWithSearchQuery(self.text!).count > 0) && (mDelegate?.textFieldShouldSelect!(self).boolValue == true) && (self.selectedText == nil){
                ...
            else{
                if let finalText = self.selectedText {
                    self.text = finalText
                    mDelegate?.textFieldDidEndEditing?(self, withSelection: ["DisplayText":finalText])
                } else {
                    mDelegate?.textFieldDidEndEditing?(self, withSelection: ["DisplayText":self.text!, "CustomObject":"NEW"])
                }
            }
        }

    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants