ios - 让自动完成快速工作

标签 ios swift autocomplete

我正在尝试实现自动完成功能,但找不到适用于 Swift 的示例。下面,我尝试转换 Ray Wenderlich's autocompletion tutorial和 2010 年的示例代码。最后,代码编译了,但是没有出现包含可能完成的表格,而且我没有经验可以看出为什么它没有被 shouldChangeCharactersInRange 取消隐藏。

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {

@IBOutlet weak var textField: UITextField!
let autocompleteTableView = UITableView(frame: CGRectMake(0,80,320,120), style: UITableViewStyle.Plain)

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

override func viewDidLoad() {
    super.viewDidLoad()

    autocompleteTableView.delegate = self
    autocompleteTableView.dataSource = self
    autocompleteTableView.scrollEnabled = true
    autocompleteTableView.hidden = true
}

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 searchAutocompleteEntriesWithSubstring(substring: String)
{
    autocompleteUrls.removeAll(keepCapacity: false)
    var indexOfPastUrls = 0

    for curString in pastUrls
    {
        let substringRange = curString.rangeOfString(curString)

        if (indexOfPastUrls  == 0)
        {
            autocompleteUrls.append(curString)
        }
        indexOfPastUrls = indexOfPastUrls + 1
    }
    autocompleteTableView.reloadData()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

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

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

    let autoCompleteRowIdentifier = "AutoCompleteRowIdentifier"
    var cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier(autoCompleteRowIdentifier, forIndexPath: indexPath) as UITableViewCell
    let index = indexPath.row as Int

    cell.textLabel.text = autocompleteUrls[index]
    return cell
}

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

最佳答案

将您的 searchAutocompleteEntriesWithSubstring 函数内容替换为以下内容。希望对您有所帮助。

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()
}

关于ios - 让自动完成快速工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26885438/

相关文章:

iOS 设置包 : Settings preserved on app upgrade?

ios - 在 Swift 3+ 中使用 NSPredicate 对自定义对象进行过滤

ios - 如何更改 BarButtonItem 的水平位置

iphone - 如何在 iPhone 上录制 AMR 音频格式?

xcode - 加载数据到 UITableView

swift - 3d 对象的静态图像 (Swift AR)

ios - 如何在iOS中管理单个项目副本中的多个项目?

autocomplete - zsh 自动完成 anaconda 环境

html - 如何快速关闭 Vim 中的 HTML 标签?

c - 如何让 Komodo Edit 7 自动完成 C?