cellForRowAtIndexPath method is not called when using autocomplete textfield

169 views Asked by At

I am working on the auto complete textfield in Swift.

I used this example:

Getting autocomplete to work in swift

But for me cellForRowAtIndexPath method is not calling at all. I don't know what mistake I have done here.

My code is like this:

 @IBOutlet weak var autocompleteTableView: UITableView!
    @IBOutlet weak var searclLocationTextField: UITextField!

    var pastUrls = ["Men", "Women", "Cats", "Dogs", "Children"]
    var autocompleteUrls = [String]()


    override func viewDidLoad() {
        super.viewDidLoad()


        autocompleteTableView.scrollEnabled = true
        autocompleteTableView.hidden = true

        // Do any additional setup after loading the view.
    }

//    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
//    {
//        autocompleteTableView.hidden = false
//        var substring = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: string)
//        
//        searchAutocompleteEntriesWithSubstring(substring)
//        return true     // not sure about this - could be false
//    }

    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
    {
        println("banana")
        autocompleteTableView.hidden = false
        var substring = (searclLocationTextField.text as NSString).stringByReplacingCharactersInRange(range, withString: string)

        searchAutocompleteEntriesWithSubstring(substring)
        autocompleteTableView.reloadData()
        return true
    }


    func searchAutocompleteEntriesWithSubstring(substring: String)
    {
        autocompleteUrls.removeAll(keepCapacity: false)

        for curString in pastUrls
        {
            var myString:NSString! = curString as NSString

            var substringRange :NSRange! = myString.rangeOfString(substring)

            if (substringRange.location  == 0)
            {
                autocompleteUrls.append(curString)
            }
        }

        autocompleteTableView.reloadData()
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return autocompleteUrls.count
    }


    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        let autoCompleteRowIdentifier = "AutoCompleteRowIdentifier"
        var cell = tableView.dequeueReusableCellWithIdentifier(autoCompleteRowIdentifier) as? UITableViewCell

        if let tempo1 = cell
        {
            let index = indexPath.row as Int
            cell!.textLabel!.text = autocompleteUrls[index]
        } else
        {
            cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: autoCompleteRowIdentifier)
        }
        return cell!
    }


    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let selectedCell : UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
        searclLocationTextField.text = selectedCell.textLabel!.text

        println(searclLocationTextField.text)
    }
1

There are 1 answers

0
sanjeet On
func searchAutocompleteEntriesWithSubstring(substring: String)
{
    autocompleteUrls.removeAll(keepCapacity: false)

    for curString in pastUrls
    {
        var myString:NSString! = curString as NSString

        var substringRange :NSRange! = myString.rangeOfString(substring)

        if (substringRange.location  == 0)
        {
            autocompleteUrls.append(curString)
        }
    }

    autocompleteTableView.reloadData()
}

you are getting finally array at this line autocompleteUrls.append(curString) after that you are reloading the tableView, so log the array count below autocompleteUrls.append(curString) this line and if you will get something definitely cellForRowAtIndexPath will be hit.