在Swift中的UITableViewCell里面添加自动完成到UITextField

时间:2016-03-27 22:47:24

标签: swift uitableview uitextfield

我按照这里的建议

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

但是当文本字段位于UIViewController内时似乎有效。在我的情况下,TF在UITableViewCell内,当有一些自动完成数据时,没有出现带有建议的表。

也未调用cellForRowAtIndexPath。其他委托方法(例如numberOfRowsInSection)正常工作。

这可能是什么原因?

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
    }


}

0 个答案:

没有答案