2

I followed the suggestion here

https://stackoverflow.com/a/32948918/5447089

But it seems to work when a Text Field is inside UIViewController. In my case the TF is inside UITableViewCell and table with suggestions doesn't appear when there is some data for auto completion.

Also cellForRowAtIndexPath is not called. Other delegate methods such as numberOfRowsInSection work normally.

What can be the reason for this?

import UIKit
import CoreData

class AddElemTableViewCell: UITableViewCell, UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var myTextField: UITextField!

    var autocompleteTableView = UITableView(frame: CGRectMake(0,80,320,120), style: UITableViewStyle.Plain)

    var elements = [“Beer”, “Bear”, “Park”, “Pad”]
    var autocompleteElements = [String]()

    var currentElem: Elem!

    override func awakeFromNib() {
        super.awakeFromNib()
        myTextField.delegate = self
        autocompleteTableView.delegate = self
        autocompleteTableView.dataSource = self
        autocompleteTableView.scrollEnabled = true
        autocompleteTableView.hidden = true

        myTextField.addTarget(self, action: #selector(AddElemTableViewCell.didChangeText(_:)), forControlEvents: .EditingChanged)
    }


    func didChangeText(textField:UITextField) {
        autocompleteTableView.hidden = false
        let substring = (myTextField.text! as NSString)
        searchAutocompleteEntriesWithSubstring(substring as String)


    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

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

        for curString in elements
        {
            let myString:NSString! = curString as NSString

            let substringRange :NSRange! = myString.rangeOfString(substring,options: [.CaseInsensitiveSearch])

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

        autocompleteTableView.reloadData()
    }

    func textFieldShouldReturn(textField: UITextField) -> Bool {
        if textField == myTextField {
            if (textField.text!.characters.count > 0) {
                let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
                let managedObjectContext = appDelegate.managedObjectContext

                let entityElement = NSEntityDescription.entityForName("element", inManagedObjectContext: managedObjectContext)

                let element = element(entity: entityElement!, insertIntoManagedObjectContext: managedObjectContext)

                element.name = textField.text!

                do {
                    try managedObjectContext.save()
                } catch {
                    let saveError = error as NSError
                    print(saveError)
                }

                textField.text! = ""
                textField.placeholder = “add new element”
                self.endEditing(true)

            }
        }
        return true
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return autocompleteElements.count
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {


        let autoCompleteRowIdentifier = "AutoCompleteRowIdentifier"
        let cell = UITableViewCell(style: UITableViewCellStyle.Default , reuseIdentifier: autoCompleteRowIdentifier)
        let index = indexPath.row as Int

        cell.textLabel!.text = autocompleteElements[index]
        return cell
    }

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


}
Community
  • 1
  • 1
orca
  • 37
  • 1
  • 8
  • 1
    You'll really need to post all your code. There are an infinite number of ways to make a programming bug. :-) – BaseZen Mar 27 '16 at 22:53
  • @BaseZen I edited the code. `AddElemTableViewCell` is a Dynamic Prototype cell. – orca Mar 27 '16 at 23:16

0 Answers0