Closed laurenswuyts closed 9 years ago
Hi,
First, make sure you set your textfield's class as AutocompleteTextfield, then make sure assign whatever the google places api response to autoCompleteStrings
.
That's exactly what I did but it still doesn't want to show it, so weird ... Is it because I have another label right underneath it? I did exactly what it says
I could help you point the problem if you will let me see how exactly you implemented it? could you show me the code?
So i have 4 textfields in a viewcontroller and one of them has a custom class: autocomplete. The one I just uploaded from your github to my project.
This is my viewcontroller:
class AddEventViewController: UIViewController, UITextFieldDelegate, AutocompleteTextFieldDelegate, NSURLConnectionDataDelegate {
var popDatePicker : PopDatePicker?
@IBOutlet weak var txtTitle: UITextField!
@IBOutlet weak var txtDate: UITextField!
@IBOutlet weak var txtLocation: AutocompleteTextfield!
@IBOutlet weak var txtDetails: UITextField!
private var responseData:NSMutableData?
private var connection:NSURLConnection?
private let googleMapsKey = "blablabla"
private let baseURLString = "https://maps.googleapis.com/maps/api/place/autocomplete/json"
override func viewDidLoad() {
super.viewDidLoad()
popDatePicker = PopDatePicker(forTextField: txtDate)
txtDate.delegate = self
txtLocation.delegate = self
configureView()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func resign() {
txtDate.resignFirstResponder()
txtTitle.resignFirstResponder()
txtLocation.resignFirstResponder()
txtDetails.resignFirstResponder()
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
if (textField === txtDate) {
resign()
let formatter = NSDateFormatter()
formatter.dateStyle = .MediumStyle
formatter.timeStyle = .NoStyle
let initDate = formatter.dateFromString(txtDate.text)
popDatePicker!.pick(self, initDate:initDate, dataChanged: { (newDate : NSDate, forTextField : UITextField) -> () in
// here we don't use self (no retain cycle)
forTextField.text = newDate.ToDateAndTimeString()
})
return false
}else {
return true
}
}
private func configureView(){
txtLocation.autoCompleteDelegate = self
txtLocation.autoCompleteTextColor = UIColor.lightGrayColor()
txtLocation.autoCompleteTextFont = UIFont(name: "HelveticaNeue-Light", size: 12.0)
txtLocation.autoCompleteCellHeight = 35.0
txtLocation.maximumAutoCompleteCount = 3
txtLocation.hideWhenSelected = false
txtLocation.hideWhenEmpty = false
txtLocation.enableAttributedText = false
var attributes = Dictionary<String,AnyObject>()
attributes[NSForegroundColorAttributeName] = UIColor.blackColor()
attributes[NSFontAttributeName] = UIFont(name: "HelveticaNeue-Bold", size: 12.0)
txtLocation.autoCompleteAttributes = attributes
}
//MARK: AutocompleteTextFieldDelegate
func autoCompleteTextFieldDidChange(text: String) {
if !text.isEmpty{
if connection != nil{
connection!.cancel()
connection = nil
}
let urlString = "\(baseURLString)?key=\(googleMapsKey)&input=\(text)"
let url = NSURL(string: urlString.stringByAddingPercentEscapesUsingEncoding(NSASCIIStringEncoding)!)
if url != nil{
let urlRequest = NSURLRequest(URL: url!)
connection = NSURLConnection(request: urlRequest, delegate: self)
}
}
}
func didSelectAutocompleteText(text: String, indexPath: NSIndexPath) {
println("You selected: \(text)")
Location.geocodeAddressString(text, completion: { (placemark, error) -> Void in
if placemark != nil{
let coordinate = placemark!.location.coordinate
}
})
}
//MARK: NSURLConnectionDelegate
func connection(connection: NSURLConnection, didReceiveResponse response: NSURLResponse) {
responseData = NSMutableData()
}
func connection(connection: NSURLConnection, didReceiveData data: NSData) {
responseData = NSMutableData(data: data)
}
func connectionDidFinishLoading(connection: NSURLConnection) {
if responseData != nil{
var error:NSError?
if let result = NSJSONSerialization.JSONObjectWithData(responseData!, options: nil, error: &error) as? NSDictionary{
let status = result["status"] as? String
println(result)
if status == "OK"{
if let predictions = result["predictions"] as? NSArray{
var locations = [String]()
for dict in predictions as [NSDictionary]{
locations.append(dict["description"] as String)
}
self.txtLocation.autoCompleteStrings = locations
}
}
}
}
}
func connection(connection: NSURLConnection, didFailWithError error: NSError) {
println("Error: \(error.localizedDescription)")
}
}
Everything works, except the tableview doesn't get shown.
It looks like you are doing it right and as what you have said, you are receiving response from google places api? From that it should work just fine. Could you send me your sample project? I want to see it along with your storyboard.
Thanks.
It's a really big project to upload but I created another one and then it does get shown but at the bottom off the view. So I think that in my original project I can't see the tableview because that subview has the wrong. Have can you make sure that the tableview gets right underneath it, because in that other project it's about 150px underneath the textfield and I have no clue why and the field itself has only a width of 50px or so..
Hi,
The tableview will always position itself right underneath the autocomplete textfield. Please double check your view heirarchy on your other project.
Hi,
I integrated your project and I can't seem to get it to work. My tableview doesn't show up. I created only a textfield in my storyboard and i'm getting response from my google places api but the tableview just doesn't show up. You have any idea if you have to do something special that isn't included in your project to get it to work?
Thanks in advance!